3/33 It looks like you printed the wrong number of items


#1

When I run the code it prints each number twice.

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

PS: I see how having two console.log(i) would do that, but what do I put for an else statement?


#2

Do you think that is the console.log that is causing too many lines to print?


#3

I thought having it written twice in the for loop might be causing it. If that isn't correct then I'm not sure. I've looked in similar threads and their code appears to be identical, or almost identical, to mine.

I ran the original for loop and it worked fine before I added the if/else statement.


#4

You are correct about that.


#5

If I write it outside the loop I get "SyntaxError: expected expression, got keyword 'else'". And If I leave else blank I get the same message as before and only the numbers divisible by 3 and 5 or both print twice.

Wait, never mind, I got it. I forgot I had to move the console.log from the top into the else nest.


#6

I asked you about this earlier ... why do you think that the console.log in your else block is the problem?


#7

Sorry I just realized what I did, I edited it into my previous reply.


#9