让我们进一步改进史蒂文的小费计算器,这次使用的是循环!
测试数据:22、295、176、440、37、105、10、1100、86和52。
提示:在循环中调用calcTip,并使用push方法向小费和总数数组中添加数值
const bills = [22,295,176,440,37,105,10,1100,86,52];
const tips = [];
const totals = [];
const bills = [22, 295, 176, 440, 37, 105, 10, 1100, 86, 52];
const tips = [];
const totals = [];
const calcTip = function (bill) {
return bill >= 50 && bill <= 300 ? bill * 0.15 : bill * 0.2;
};
for (let i = 0; i < bills.length; i++) {
tips.push(calcTip(bills[i]));
totals.push(bills[i] + tips[i]);
}
console.log(tips);
console.log(totals);
const arr = [22, 295, 176, 440, 37, 105, 10, 1100, 86, 52]; const calcAgerage = function (arr) { let sum = 0; for (let i = 0; i < arr.length; i++) { sum += arr[i]; } return sum / arr.length; }; console.log(calcAgerage([1,6,7,50,30]));