So close! Won't print FizzBuzz for 15


#1

I'm so close. And I figured it out myself which is totally amazing. I might actually be learning to program after all. So here it is and it just won't print out FizzBuzz for 15. Gosh I'm gonna be slapping my forehead.

for (i = 1; i < 21; i++)

if (i % 3 === 0){

console.log("Fizz")

}

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

else if (i % 5 === 0 && i % 3 === 0) {

console.log("FizzBuzz")

}

else {

console.log(i)

};


#2

It's because it goes in order
it goes like this
is "i % 5" equal to "0"
and if it is 15 that would be true. so it would ignore all the other if/else if/else's in there and just do where it first occurs.

So you need to rearrange the order of the checks so it checks if it can be modulo-ed by both. :smile:


#3

Try switching:

if (i % 3 === 0){

and

else if (i % 5 === 0 && i % 3 === 0){

But keep the else if and if statements where they are.


#4

Beat me to it :stuck_out_tongue:


#5

Actually it looks like you beat me to it. But I will shake your hand either way good sir.


#6

use this:
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

Thank you both. I'm now starting to think like a programmer thanks to your help.

All the best,

Mark


#8

Yes, you have to do FizzBuzz first, then Fizz, then Buzz.


#9

This is what I did...

var i = 1;
while (i < 21) {
//do stuff
if (i % 5 === 0 && i % 3 === 0){
console.log("FizzBuzz");
}
else if (i % 3 === 0) {
console.log("Fizz");
}
else if (i % 5 === 0){
console.log("Buzz");
}
else {
console.log(i);
}
i ++;
}


#10

I actually ran into the same problem. Here's what I did to fix it:

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

The order is important here. Make sure to put the "FizzBuzz" in the beginnnig of the if/else statement.


#11

This is my example and it work, but the course just don't let me to continue. What is wrong? Please, help me!
Sorry! My mistake! The second "IF" must be "ELSE IF".

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

};


#12

check your second if statement and replace it by else if.


#13

是的呢,非常对的呢,我想了好长时间,代码段的逻辑变换一下顺序,整个代码的意思就会变哦


#14

Try puttting every thing inside the for loop like so
for(i=1; i<=20; i++){

if(i%3==0&&i%5==0) {
// Do one thing here
console.log("FizzBuzz");
}
else if(i%5==0) {
// Do another thing here
console.log("Buzz");
}
else if(i%3==0){
console.log("Fizz");
}
else{
console.log(i);
}
};


#15

I did the stupider..well, less efficient thing. I guess I'm not gonna last long... :sweat:

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


#16

I love u nigga! <3
hahahahah