Oops, try again You. printed Fizz when you should have printed FizzBuzz


#1

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

#2

it is because when i=15

It checks

if(15%3===0) {
console.log("Fizz");
}

that is true. so it prints Fizz. if first condition is false then it'll check second condition if it's too false then it'll check third condition if it's too false then it'll print else statement.but if first condition is true then it'll not go in second condition.


FizzBuzz Exercise
#3

Checking for if ( i % 3 === 0 && i % 5 != 0 ) should help.


#4

Thanks all. I just put the first if(i%3===0 && i%5===0) and its work


#5

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