I was working on some codes for beginners and I come across a task to calculate a tip, given a bill, using a function and an array. When I see the result I see that 555 was applied with the x 0.15 even though it the bill isn’t a number between 50 and 300. Why is that?
const calcTip = bill => {
if (bill >= 50 || bill <= 300){
return bill * 0.15
} else {
return bill * 0.20
}
};
const bills = [125, 555, 44];
const tips = [calcTip(bills[0]), calcTip(bills[1]), calcTip(bills[2])];
console.log(tips[1])
>Solution :
With the check
if(bill >= 50 || bill <= 300)
you are multiplying bill to 0.15 when it is greater than 50 OR less than 300. This mean always.
You meant
if(bill >= 50 && bill <= 300)
Translating in English it sounds like "If the bill is greater then the minimum AND it is less than than the maximum…".