3. ...And the good!


#1


I followed all the reqs for this question but Im still getting an error stating "Oops, try again. You printed Fizz when you should have printed 1" Im at a loss as to why Fizz is printing first and foremost.

Here is the console output:
Fizz
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
Fizz
16
17
Fizz
19


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

};


#2

Hey there,

Fizz is printing first because the value of i to start off with is 0.

0 % 3 = 0 therefore Fizz will be printed.

You'll want to initialize i = 1 so this this problem can be avoided.


#3

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

};

this prints out : 1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
Fizz
Buzz
FizzBuzz
16
17
Fizz
19

I can't understand what should i do if not this


#5

1, use ' else if ' ...
2. also change the order ie. check first... if(i%5===0 && i%3===0 ){console.log('FizzBuzz') }
else if.. (the remaining).


#6

This is the right answer.

for (i=1; i<=20; 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");
    } else {
        console.log(i);
    }
};

Alright, so here are some problems you came across :slight_smile:
1. You didn't use if-else if-else formula
2. Do that in the order of (if both conditions are true, do this; else if one condition is true, else if the other condition is true, else print out the number) Just like my example above.
3. Another problem seems to be on the code you stated to stop the iteration. Yours was i<20. It is supposed to be i<=20. Because the instruction wants you to loop through the number 1 - 20. So = will include 20. Without =, it will stop just right before you hit 20, meaning 19.

Also, you don't need this piece of code.


#7

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