Home > Software engineering >  Invalid left-hand side in assignment error. Can't find the mistake
Invalid left-hand side in assignment error. Can't find the mistake

Time:08-14

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

CodePudding user response:

In this piece of your code

var result = (x % 3 === 0 && x % 5 === 5 = 0)

you have at right = 0 this means, "Hey You at LEFT" receive this value, but, you have not a regular variable to assign it at LEFT SIDE, so javascript understand that you want assign 0 to a comparison, x % 3 === 0 && x % 5 === 5 and we know that it is not possible because this evaluates to a boolean (true or false), you can't express: true = 0. This is the reason why this exception was throwed

  • Related