3 ...And the good!


#1



Unsure about this.error here, can somebody guide me a little.


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


#2

You can only use 1 if statement in an if/else statement. Try changing it to this...

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

Also, move the "FizzBuzz" statement so it's first and it should work. Hope this helps @33hawk :slight_smile:


#3

Thanks! finally it worked, in my defence I could say my code was printing the results as they should.

This is the final one

`for(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);
}
}`

#4

Yea there's usually multiple ways to get it to to do what you need it to! The grader is really picky most of the time XD Just let us know if you need any more help!


#5

I will most likely need it thanks a lot!
If your voice can reach the moderators can I'd like to suggest to integrate linter && beautify, that would be super handy?


#6

Guess I've read it somewhere on the to-do list but don't expect it to be enabled fast :frowning:
Also in this case it is not about the grader being picky or about things that a linter or beautifier could solve. The problem is that the results differ from the expected.

If you have a look at your first approach and go through it with a value of 15 you'd get 3 result because all "Fizz", "Buzz" and "FizzBuzz" are printed. If you'd now go with the suggestion to replace the 3 ifs with a row of if/else if ... then you'd only get one output but that would be the first matching case: "Fizz".

So this is only true for numbers that are not divisible by 15.


#7

I tried it in the chrome console and you are so right, it doesn't print as it should.
About linter and beautifier I just saw it on codecombat and my opinion is that they are useful, mostly beautifier.