var allBills = [124,48,268,180];
var tips =[];
//var total = [];
document.write(allBills);
function calcIt(bill){
for (i=0; i < bill.length; i++){
if(bill[i] < 50){
tips[i] = bill[i] * 20% + " ";
}else if(bill > 50 && bill <200){
tips[i] = bill[i] * 15% + " ";
}else {
tips[i] = bill[i] * 10% + " ";
}
return tips += tips[i];
};
document.write(calcIt(allBills));
我正在尝试为每个餐厅帐单加倍 var allBills = [124,48,268,180] ; ,为什么这不起作用?
答案 0 :(得分:2)
100
或简单地按如下方式使用百分比:100 = 1, 90 = .9, 50 = .5, 15 = .15, 5 = .05
,依此类推。 我只是为了便于阅读而使用toFixed(2)
var allBills = [124, 48, 268, 180];
function calcIt(bill) {
var tips = [];
for (i = 0; i < bill.length; i++) {
if (bill[i] < 50) tips[i] = (bill[i] * .2).toFixed(2) + " ";
else if (bill > 50 && bill < 200) tips[i] = (bill[i] * .15).toFixed(2) + " ";
else tips[i] = (bill[i] * .1).toFixed(2) + " ";
}
return tips;
}
document.write(calcIt(allBills).join(" | "));
答案 1 :(得分:0)
您正在使用提醒操作符(%),它提供了提醒。您的逻辑与您的期望不符。如果要将%符号添加到输出中,则可以将其用作字符串