Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is javascript ignoring second if statment?

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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…".

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading