Fizzbuzz


#1

for (var 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);
}

}

Error: You printed Fizz when you should have printed FizzBuzz

I can't figure out what the problem is.

Thanks for any help.


I'm having problem with my FizzBuzz
#2

for i=15, i%3==0 is true, so fizz is logged to the console. Since if was true, else if and else will not be executed, so the loop moves on to the next number


#4

Hi, I was stuck and after I read your reply I just moved the condition with the comparison to the beginning of my code, and then It worked. Thanks.


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.