Code behaves correctly, but I get an error


#1


https://www.codecademy.com/courses/spencer-sandbox/0/3?curriculum_id=506324b3a7dffd00020bf661#

"Oops, try again. You printed 3 Fizz when you should have printed Fizz."

In the console I get the correct answer, so:

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

And this is my code:

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

I do not understand where is my mistake, or what I am missing.
Your help will be highly appreciated.


#2

You don't have to add i while printing them.
Like one of the case here..

console.log(i + " FizzBuzz")

You have to just print "FizzBuzz" , "Fizz" and "Buzz" when certain condition meets.


#3

Thanks! I thought it was supposed to follow the number... my bad. I get it now. Thank you again.


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.