And the good...issues! I am so confused


#1

So, I am currently trying this code out:

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

and the error i get is "Oops, try again. It looks like you printed out the wrong number of items."
Can anyone please tell me what it is that I'm doing wrong? the console is showing fizz but I'm so stuck and confused.


#2

Hi ibrierley90,

Javascript code is executed line by line. For example, suppose i=15. When you run your code first it will check the first if statement, i.e., if (i % 3 === 0). This condition results true, hence that if block gets executed and the console log will print "fizz". But 15 is also divisible by 5, that's why the result should be "FizzBuzz". That's why your code is getting an error. So first you have to write the if condition where it checks whether the number is divisible both by 3 and 5. This will give you the correct solution.


#3

you left out the curly braces after the for loop. code should read

for(var i = 1; i <= 20; i++){ //stuff to do }

and not

var (i = 1; i <= 20; i++); //stuff to do

and there is also a problem with the FizzBuzz condition.


#4

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