# What's wrong with this program?

#1

for (var i=0;i<=20;i++)
{
if ( i % 3===0 )
{
console.log("Fizz");
}
else if (i%5===0 )
{
console.log("Buzz");
}
else if ((i%3 && i % 5 )===0)
{
console.log("FizzBuzz");
}
};

#2

Here are the instructions for the FizzBuzz assignment:

Print out the numbers from 1 - 20.
For numbers divisible by 3, print out "Fizz".
For numbers divisible by 5, print out "Buzz".
For numbers divisible by both 3 and 5, print out "FizzBuzz" in the console.
Otherwise, just print out the number.

Numbers not divisible by 3 or 5 (like 1, 2, 4, 7... ) aren't being printed at all.
Numbers divisible by 3 and 5 (just 15 in this range) are printing "Fizz" instead of "FizzBuzz"

#3

The procedure of each output should be:
1: See if var i is divisible by 3 and 5, if true, execute FizzBuzz, else go to number 2.
2: See if var i is divisible by 3, if true, execute Fizz, else go to number 3.
3: See if var i is divisible by 5, if true, execute Buzz, else execute var i.

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

#4

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