I'm trying this code in javascript. but it's giving me this error.
var x = 1;
while (x <= 20) {
var result = (x % 3 === 0 && x % 5 === 5 = 0) ? "FizzBuzz" : (x % 3 === 0) ? "Fizz" : (x % 5 === 0) ? "Buzz" : x);
console.log(result);
}
I thought that it was due to me assigning 'x' which is a number to 'result' which was at some point a string, so I tried to replace the final 'x' with a string, same thing.
I know I can write this with normal if-else statement but I would like to know how to do it with inline conditionals
In this part of your code
var result = (x % 3 === 0 && x % 5 === 5 = 0) has right = 0 , this means, "Hey, you on the LEFT" receives this value, but doesn't have a regular variable to assign to it on the LEFT SIDE, so javascript understands that you want to assign 0 to a comparison, x % 3 === 0 && x % 5 === 5 and we know it's not possible because this evaluates to a boolean value (true or false), it can't be expressed: true = 0 . This is why this exception was thrown.