簡體   English   中英

如何在數組中保存並存儲來自 function 計算的計算值

[英]how to save and then store a calculated value from a function calculation, inside an array

我不能比這個線程的 header 更強調這個問題,我正在嘗試將先前計算的總和從 function 保存到我之后創建的空數組中(在函數范圍之外)。

我怎樣才能將所有計算出的總和保存到一個數組中,而不是僅將計算出的元素推入數組中,下次有新元素時,前一個元素將被刪除而不保存。

還!! 我想知道我是否正確編寫了任務並正確使用了function工具!

史蒂文仍在構建他的小費計算器,使用與以前相同的規則:如果賬單價值在 50 到 300 之間,則小費為賬單的 15%,如果價值不同,則小費為 20%。 你的任務:

  1. 編寫一個 function 'calcTip',它將任何賬單值作為輸入並返回相應的小費,根據上述規則計算(如果需要,您可以查看第一個小費計算器挑戰中的代碼)。 使用您最喜歡的 function 類型。 使用賬單價值 100 測試 function
  2. 現在讓我們使用數組! 所以創建一個包含下面測試數據的數組'bills'
  3. 創建一個數組“tips”,其中包含每個賬單的小費值,根據您之前創建的 function 計算得出
  4. 獎勵:創建一個包含總值的數組“total”,因此 bill + tip 測試數據:125、555 和 44 提示:記住數組需要每個 position 中的一個值,而該值實際上可以是一個返回值function,所以你可以調用 function 作為數組值(所以不要先將小費值存儲在單獨的變量中,而是在新數組中)

我的代碼:

myBills = [125, 555, 44, 57, 683, 12, 991, 33, 477, 28, 1215];
const billCalc = Math.floor(Math.random() * myBills.length);
const randomBill = myBills[billCalc];
let tipValue = undefined;
if (randomBill >50 && randomBill <300) {
    tipValue = 15;
}
else if (randomBill <50 || randomBill >300) {
    tipValue = 20;
}
let finalTip = tipValue / 100 * randomBill;

function calcTip(tip) {
    if (tipValue === 15) {
        console.log(`The bill for the table is ${randomBill}, 
        and the tip is : ${finalTip}.
         The final payment is: ${randomBill + finalTip}`);
    }
        
    else if (tipValue === 20) { // change the rquality operator to 2 or 3
        console.log(`The bill for the table is ${randomBill}, 
        and the tip is : ${finalTip}.
         The final payment is: ${randomBill + finalTip}`);
    }

}

// this supposed to save the results for the tips, and to store it in the array below this line.

const tipSave = calcTip(finalTip);

const tipList = [21, 22, 63]
tipList.push(tipSave);

如果你能從我的代碼中教我什么,我做錯了什么,我如何簡化它,讓它更干凈,或者我用過和不應該用的東西。

還有關於數組的問題。 我非常想得到這方面的幫助。

看着寫的東西。。。。

// myBills = [125, 555, 44, 57, 683, 12, 991, 33, 477, 28, 1215];
// it's a good habit to declare things that shouldn't change as const
const myBills = [125, 555, 44, 57, 683, 12, 991, 33, 477, 28, 1215];

你寫了一個測試很好......

const billCalc = Math.floor(Math.random() * myBills.length);
const randomBill = myBills[billCalc];

...但是,下一點是要測試的邏輯,您被要求以函數形式編寫它。 這似乎是整個練習的重點,因此將您的代碼移至 function 並稍微調整一下......

function calcTip(amt) {
    let tipValue; // don't need = undefined;, that's the default
    // the question is unclear whether these are >= and <=
    // the norm is inclusive on the low end, exclusive on the high
    if (amt >= 50 && amt <300) { 
        tipValue = 15;
    }
    else {
     // we don't need to spell out the alternative with else if 
     // and the alternative you spelled out:
     // <50 || >300 misses edge cases when the amount is 50 or 300

        tipValue = 20;
    }
    let finalTip = tipValue / 100 * amt;
    return finalTip;
}

壓縮所有內容同樣正確且幾乎同樣易於閱讀,如下所示:

function calcTip(amt) {
  const tipValue = amt >= 50 && amt < 300 ? 0.15 : 0.20;
  return tipValue * amt;
}

現在我們可以編寫您的測試...

console.log(`the tip for ${randomBill} is ${calcTip(randomBill)}`)

通過檢查和手算來檢查這一點。

在 JS 中應用到數組很容易。 您只需要了解map()

array.map(function)

Map 采用單個參數 ( function ) 並將該 function 應用於array的每個元素,返回 function 為每個元素返回的任何數組。

這正是您所需要的。 你的數組是myBills ,你的 function 是calcTip ,所以:

const tips = myBills.map(calcTip);
console.log(tips)

獎勵:總數 function 可以直接使用 calcTip function

function calcTotal(amt) {
    return amt + calcTip(amt);
}

const totals = myBills.map(calcTotal;
console.log(totals);

大多數人習慣於將 function 參數編碼為內聯 map。 這是一個更深層次的主題,但請注意,我們可以跳過創建calcTotal function 並直接編寫:

const totals = myBills.map((amt) => {
  return amt + calcTip(amt);
});

或者,更簡潔地...

const totals = myBills.map(amt => amt + calcTip(amt));

總之:

 const myBills = [125, 555, 44, 57, 683, 12, 991, 33, 477, 28, 1215]; function calcTip(amt) { const tipValue = amt >= 50 && amt < 300? 0.15: 0.20; return tipValue * amt; } const tips = myBills.map(calcTip); console.log(tips) const totals = myBills.map(amt => amt + calcTip(amt)); console.log(totals)

我正在嘗試將先前計算的總和從 function 保存到我之后創建的空數組中

抱歉,我不確定我是否理解正確,因為我在您的代碼中沒有看到任何空的 arrays,但是我冒昧地假設您要向其添加元素的數組是tipList

我怎樣才能將所有計算出的總和保存到一個數組中,而不是僅將計算出的元素推入數組中,下次有新元素時,前一個元素將被刪除而不保存。

通過使用 yourArrayName.push(),元素被附加到數組的末尾,並且數組中已有的元素保持不變。

編寫一個 function 'calcTip' 將任何賬單值作為輸入

您的 function calcTip()將參數“提示”作為輸入。 通常最好參考它代表的輸入來命名參數,也許是“bill”。

我不太確定你想用你的代碼得到什么,所以我會提出一些建議:

  • else if 行不是必需的,因為您只是在測試 if 條件的反義詞,else 關鍵字就足夠了
  • 將小費的計算放在 function 之外是不可取的,因為小費對於后續調用calcTip()是一個常數,將其放在 function 中以獲得准確的小費率
  • calcTip()采用tip參數,但該值未在 function 中的任何地方使用,而使用在 function之外計算的 static 值。 使用這些 static 值沒有問題,但是應該使用tip參數使您的 function 更“適用”於不同的輸入值

我對您的代碼進行了一些更改並添加了一些注釋以供您理解,並在底部包含了一些參考資料以獲取有關我使用的方法和關鍵字的更多信息

 myBills = [125, 555, 44, 57, 683, 12, 991, 33, 477, 28, 1215]; const billCalc = Math.floor(Math.random() * myBills.length); const randomBill = myBills[billCalc]; function calcTip(bill) { // function which gives us the tip based on the instructions, we define this function below const percent = getMultiplier(bill); // calculation of the tip ( bill x tip% ) const finalTip = bill * percent / 100; console.log(`The bill for the table is ${bill}, and the tip is: ${finalTip}. The final payment is: ${bill + finalTip}`); return finalTip; // this line gives tipSave the value of the finalTip at line 29 } // this function which tests if the argument "bill" is within the range of 50 < bill < 300 // If it is within the range, it returns 15, otherwise, it returns 20 function getMultiplier(bill) { if (bill > 50 && bill < 300) { return 15; } else { return 20; } } // this supposed to save the results for the tips, and to store it in the array below this line. const tipSave = calcTip(randomBill); const tipList = [21, 22, 63]; tipList.push(tipSave); console.log(tipList); // prints the array of tips to the console

一些參考資料

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM