0

Here is a link to my codepen. https://codepen.io/julius-88/pen/poRNRrN?editors=1010

let bill = 430;
const tip = bill * 0.15;
const tip2 = bill * 0.2;
const finalBill = bill + tip;
const finalBill2 = bill + tip2;

tip >= 50 <= 300 ? console.log(`The bill was ${bill}, the tip was ${tip}, and the total value is ${finalBill}`) : console.log(`The bill was ${bill}, the tip was ${tip2}, and the total value is ${finalBill2}`);

My problem is the return value. I have checked all of my const individually and they are all correct. But when the final result shows it shows the wrong number.

In the example in my codepen the tip is suppose to be 86 but it says that it is 64.5. I have no idea why it reduced the number and im going nuts trying to figure it out.

Does anyone know why?

Julius
  • 21
  • 5
  • Did you mean to write `tip >= 50 && tip <= 300` ? – Igor Mar 30 '21 at 16:07
  • 1
    tip >= 50 <= 300 is not a proper check. Use ```tip >= 50 && tip <= 300 ``` – Bulent Mar 30 '21 at 16:07
  • `finalBill = bill * (tipPercent + 1)` does the same thing but in one line. But 15% of 430 is 64.5 and 20% is 86. So what's wrong? – Richard Barker Mar 30 '21 at 16:08
  • Can you try using this condition tip <= 50, as anyways you are paying the second tip and the 300 case might be neglected – SARAN SURYA Mar 30 '21 at 16:14
  • So I dont know what to do about the duplicate error. but you guys are correct that I should add the && but apparently I was also suppose to change the tip to bill. it works now. thanks for your help. – Julius Mar 30 '21 at 16:20

1 Answers1

0

You've got a syntax error in your JavaScript right here:

tip >= 50 <= 300

Operators in JavaScript with similar precedence will be interpreted left-to-right. So this will first determine if tip is greater than or equal to 50, and return a boolean true or false. It will then see if true or false is less than or equal to 300.

JavaScript implicitly converts types when there's an operator mismatch, so when using an inequality operator on a boolean it will be converted as:

  • true = 1
  • false = 0

In either case true (1) <= 300 and false (0) <= 300 so no matter what the value of tip your conditional will always return true.

I think what you mean to say was:

tip >= 50 && tip <= 300 ? ...

Due to operator precedence the inequalities will be processed first (kind of like saying 2 * 3 + 4 * 5, the multiplication will be processed before the addition). It will then use the && to check that both inequalities were true.

stevendesu
  • 15,753
  • 22
  • 105
  • 182
  • 1
    `50 <= tip <= 300` isn't any special syntax. Just like the original bug, it will evaluate left to right. `50 <= tip` will result in `true` or `false`, then `true` (1) or `false` (0) will always be less than 300 so it will always return `true` from the whole statement regardless of the value of `tip` – stevendesu Mar 30 '21 at 17:26
  • 1
    @RichardBarker try it for yourself in the Chrome console or in Node: `50 <= 25 <= 300` returns true. `50 <= 250 <= 300` returns true. `50 <= 350 <= 300` returns true. Combining two inequality operators is "valid JavaScript" but it's almost always a bug since you're implicitly converting a boolean to a number. – stevendesu Mar 30 '21 at 17:30