3. "...And the good"


#1

I am really confused,I don't know what I am doing wrong.This is my error:
"Oops, try again.
It looks like you printed out the wrong number of items.
"
And here is my code:
for(var i = 1; i<21; i++){
if(i % 3 === 0){
console.log("Fizz");
}
else if (i % 5 === 0){
console.log("Buzz");
}
if(i % 3 === 0 && i % 5 === 0){
console.log("FizzBuzz");
}
else {
console.log(i);
}
}


#2

Hi your if , else if and else statement should have that structure

if (conditon) {

}
else if (conditon) {

}
else if (conditon) {

}
else {

}

#3

I checked,and it does.Are there any mistakes in my code?


#4

here

if(i % 3 === 0 && i % 5 === 0){
console.log("FizzBuzz");
}

instead of if it should be else if


#5

I tried this:).
This is the error:

You printed Fizz when you should have printed FizzBuzz

#6

try to put first this part

if(i % 3 === 0 && i % 5 === 0){
    console.log("FizzBuzz");
}

the the others parts like that

for(var i = 1; i<21; 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);
    }
}

#7

Oh thanks alot,it works:).


#8

I had this same thing happen to me. Put your last if statement as the top of the list. The way it's written now, your computer will see 15 is divisible by 3 and then not go back to see if it's also divisible by 5 because it's already been assigned something. I think that's what happens, I'm still trying to figure it out myself. All I know is when I put (i % 3 == 0 && i % 5 == 0) first, it fixed the problem.


#9

you have to put mod3 AND mod 5 first because otherwise you will have entered into a statement (mod 3 or mod 5) and will never make it to the mod 3 AND mod 5 condition