A problem with FizzBuzz


#1

HI! I need help with my code. Here it is:
for (var i = 1; i < 21; i++){
console.log(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")
}
};

And it is correct (I think) because it prints out:
1
2
3
Fizz
4
5
Buzz
6
Fizz
7
8
9
Fizz
10
Buzz
11
12
Fizz
13
14
15
FizzBuzz
16
17
18
Fizz
19
20
Buzz

But, there's and error saying:
Oops, try again. It looks like you printed out the wrong number of items.

What's the error? I can't find any. Thank you!


#2

Hi you missing the else statement and you should remove this part console.log(i) after the for loop and put it inside the else statement.


#3

Thanks! but I don't get why I needed to do that, can u explain? Also, why did was 20 not printed?

1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz


#4

Because if you put console.log(i) you will print the number from 1 to 20, but the lesson gave you some rules so that why when you print all the number the the code will see that as an error.

Then why 20 dont get printed?
Its because 20 divide by 5 have as remain 0 , this part

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

That why the last thing printed is "Buzz".


#5

Ohhh. I get it. Thank you.


#6

nfvm, found the error