Here is the code:
var i = 0;
for (i = 1; i <= 20; i++) {
if (i % 5 === 0 && i % 3 === 0) {
console.log("FizzBuzz");
} else if (i % 5 === 0) {
console.log("Buzz");
} else if (i % 3 === 0) {
console.log("Fizz");
} else {
console.log(i);
}
}
and this is the output:
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz
The code is ok and works well but I don't understand why does it only print FizzBuzz
when logical operator && is in if
statement and not in else if
?
Aucun commentaire:
Enregistrer un commentaire