3/5 Code bug?


#1

var number = 0

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

this code don't work. Why?


#2

When 'number' is '15', which is divisible by 3 and 5, your code will print 'Fizz'. That is the first condition computer run, and because its true, its going to skip rest of the code and go for another number.

Here is solution if you want to check

 var number = 1;

while (number <= 20) {
if (number % 5 == 0 && number % 3 == 0) {
    console.log('FizzBuzz');
    }
else {
if (number % 3 == 0) {
    console.log('Fizz');
    }
else if (number % 5 == 0) {
    console.log('Buzz');
    }
else {
    console.log(number);
    }
    }
number +=1
}