Fizz buzz meltdown - unexpected token


#1

Can anyone shed any light on why this isn't working? It returns SyntaxError: Unexpected token )

I might be totally on the wrong track, but any pointers are welcome.

for (i = 1, i <20, i++) {

if (var answer = i % 3 == 0) {
    console.log("Fizz");

    }

     else if (var answer = i % 5 == 0) {

        console.log("Buzz");

    }

    else if (var answer = i % 3 == 0) && (var answer = i % 5 == 0) {
        console.log("FizzBuzz");

    }

    else {
        console.log(i);
    }

};


#2

Wowowow, Hey @bennaby ! You got a couple problems.

First when you do an if statement you dont declare it inside. Let me show you one of your problems inside it.

One of your code(multiple like this so fix them):

Just do it like this

if (i % 3 == 0)

same with else if dont declare your variables. I recommend you go and look back the javascript tutorial since you need a little bit of refreshment, just in my oppinon

Hope it helped if it didnt just reply saying so! Cheers!


#3

Thanks for the fast answer.. you're right I'm struggling here!

OK I've amended the code and come up with this. It works but doesn't log "FizzBuzz" for i= 15. Am I using the wrong operator?

Here's the code for that section:

else if ((i % 3 == 0) && (i % 5 == 0)) {
console.log("FizzBuzz");
}

and here's the full code:

for (i = 1; i < 21; i++) {

if  (i % 3 == 0) {

    console.log("Fizz");

    }

     else if (i % 5 == 0) {
        
        console.log("Buzz");
     }
        
  
    else if ((i % 3 == 0) && (i % 5 == 0)) {
        console.log("FizzBuzz");  
        }
        
   
    
    else {
        console.log(i);
    }

};


#4

OH I got it! I just needed to put the 'FizzBuzz' if else before the other ones.

Thanks for your help.


#5

No Problem man! Glad to help


#7

what worked form ne

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

}


#8

Here's mine short and sweet. I make the loop log "FizzBuzz" at 15 first, then come back around for the rest of the numbers:

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

#9

Here's mine I hope this helps someone.

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