I can't seem to debug this code


#1

for (1 <= x <= 20) {
if (x % 3 == 0) {
console.log("Fizz");
}

else if (x % 5 == 0) {
console.log("Buzz");
}

else if (x % 3 == 0 && x % 5 == 0) {
console.log("FizzBuzz");
}

console.log(x);

}


#2

First look at your for statement.

Here you have a example how a for statement should look like:

for (var i = 0; i < 5; i++) {
   // Other statements
}

#3

Even with the for statement correct, the code will never log FizzBuzz. That is because both the 3 and 5 cases are tested before the 3 AND 5 case, which should be tested first, and make the first branch (given the present logic).

for (var i = 1; i < 21; i++) {
    if (i % 15 === 0) { // same as (i %3 ===0 && i % 5 === 0)
        console.log("FizzBuzz");
    } else
    if (i % 3 ===) { // and so on...
}