javascript习题

it2026-03-09  5

javascript的练习习题

小明和他家人在泰国旅游,到3个不同的饭店吃饭。账单(bill)分别是124元、48元和268元。 为了给服务员小费(tip),小明创建了一个简单的小费计算器函数(tipCalculator)。如果账单小于50元,他会给账单的20%作为小费﹔如果账单在50到200元之间,他会给账单的15%作为小费﹔如果账单超过200元,他会给账单的10%作为小费。 小明想要⒉个数组:1)一个数组包含所有三个小费(每个账单一个)﹔2)一个数组包含所有三个最终支付的金额(账单+小费)。

/** *

@param {number[]} bills */

const tipCalculator = bills => { const tips = [] const costs = [] bills.forEach(bill =>{ let tip; if (bill < 50){ tip = bill * 0.2 }else if (bill >= 50 && bill < 200){ tip = bill * 0.15 }else{ tip = bill * 0.1 } const cost = bill + tip tips.push(tip) costs.push(cost) }) console.log(tips,costs) } tipCalculator([124,48,268])

`

最新回复(0)