3/33 ReferenceError: "Invalid left hand side expression in postfix"


#1
for (var i=1; i<=20; 1++){
    if ((i%3===0)&&(i%5===0)){
        console.log("FizzBuzz")}
    else (i%3===0){
        console.log("Fizz")}
    else if (i%5===0){
        console.log("Buzz")}
    else {
        console.log(i)}
}

the error i'm getting is ReferenceError: "Invalid left hand side expression in postfix"

what does that mean?


#2

One thing I'm noticing is that
else (i%3===0){
console.log("Fizz")}
should be
else if (i%3===0){
console.log("Fizz")}
other than that I have no clue.


#3

@savvybosschick,
apart from the case @corepro16878 put forward (else should be else if),

In

for ([initialization]; [condition]; [final-expression/postfix]) {
   statement
}

you are using 1++ instead of i++
as 1++ stands for 1 = 1 + 1 you will get "Invalid left hand side expression in postfix"


#4

This may help us:

for(var i=?;i<=?;i++){
   if(){
   } else if(){
   } else if(){
   } else{
   };
};

i++ instead of 1++.


#5

Please read code convention IF ELSE-IF
http://javascript.crockford.com/code.html


#6

If you (like I), have a fear of links for any reason (viruses), whether that fear is warranted or not (it's not), @leonhard_wettengmx_n is saying there shouldn't be a semi-colon on any if - else if - else statement. Hope it helps!


#7

First of all, I think your loop is not correct:

for (var i=1; i<=20; 1++)  {  }

I think you should write like this:

for (var i=1; i<=20; i++) { }

:no_mouth:


#8

im stuck on it too i dont get it


#9

you should put I++ and an else if


#10

thank you , but which semi-colon:

      };      ?
};            ?

#11

The first closing bracket shouldn't have a semi-colon, as that's part of the if statement. I wouldn't suggest having either of those semi-colons though, because in reality you'd only use the semi-colon after a closing bracket when:

  • Ending and Object

var Example = { };
  • Ending an initialized variable function

var Example = function(){ ... };

Other than that I believe you shouldn't use the semi-colons after the closing brackets. Somebody please correct me if I'm wrong.


#12

Yeah. i guess you are right lolman , thanks a lot @lolman