Something is wrong


#1


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


#2

Well in case of 15 you'll probably see "Fizz" instead of "FizzBuzz". Due to the order in which you go through your conditions.


#3

then what should i do to fix it?


#4

Well in an if/else if/else statement the first matching condition is picked. So as far 15 all 3 conditions are true but %3 comes first, "Fizz" is printed. If you'd change the order so that the "FizzBuzz" check comes first you could get the correct result.


#5

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