Section 2.5: CODDING CHALLENGE 2

John and his family went on a holiday and went to 3 different restaurants. The bills were $124, $48 and $268.

To tip the waiter a fair amount, John created a simple tip calculator (as a fucntion). He likes to tip 20% of the bill when the bill is less than $50,
15% when the bill is between $50 and $200, abd 10% if the bill is more than $200.

In the end, John would like to have 2 arrays:
a. Containing all three tips (one for each bill).
b. Containing all three final paid amounts (bill+ tip).

(NOTE: To calculate 20% of a value, simply multiply if with 20/100 = 0.2)

Good Luck

function tipCalculator(bill) {
    var percentage;
    if (bill < 50) {
        percentage = 0.2;
    } else if (bill > 50 && bill < 200) {
        percentage = 0.15
    } else {
        percentage = 0.1
    }
    return percentage * bill
}

console.log('The tip is $' + tipCalculator(10));

var bills = [124, 48, 268];

var tips = [tipCalculator(bills[0]),
            tipCalculator(bills[1]),
            tipCalculator(bills[2])]
// You also can do
// var tips = [tipCalculator(124),
//             tipCalculator(48),
//             tipCalculator(268)]     

var finalBill = [bills[0] + tips[0],
                 bills[1] + tips[1],
                 bills[2] + tips[2]];
console.log('Tip is: '+tips, 'Final Bill is: ' + finalBill);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值