3/33 fizzbuzz stopping at fizz


#1

Ok, I cannot figure out why this only runs to fizz and then stops.
Anyone see what I am missing?

for(i=1;i<=20;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);
    }
};

#2

For the Javascript-Interpreter
the semi-colon-; is an End-of-Statement indicator.
Everything AFTER the semi-colon-; will NOT be interpreted.

You are using a semi-colon-; directly after the FOR-condition
like

for(i=1;i<=20;i++);

Remove that semi-colon-;.


#3

That's actually not correct, it is interpreted. it is just interpreted as a new statement which in this case is a problem as the first statement after a for loop should be {...}.


#4

This is what your code should look like:
for (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);
};


#5

This code doesn't work


#6

no,

the semi-colon after the second bracket has nothing to do there.


#7

The reason you should not use a semicolon there. Well the {} after loops and conditions are rather optional without them they would just work on the very next statement. So what they do is wrap all your statements into 1 single statment, so they're necessary for 2 or more but not for 1 statement. What happens now is that when you use a semicolon, the semicolon becomes this 1 statement something like:

/*Nothing to do here... aaaand I'm done*/;

So what the loop does is counting till 20 while doing nothing and after this is done going through the stuff in {} which is treated as regular code. As the first value of i to fail the condition is 21 you proceed with 21 which gives you the "fizz" message. Hope this helps to understand what's going on here.


#8

The reason jrpsychgal's code didn't work is because his for statement was messed up. This is the proper code-
for (var i = 1; i <= 20; 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

I can't believe a guy did the same mistake and said it was the answer.
Anyways I think jefftheguyperson is correct


#10

It worked for me... Except for the semicolon. How did I type that and miss it?! :confused: