Wierd Error


#1



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


The syntax is correct, but when it runs, it does not correctly use 'fizzbuzz'. it just prints 'fizz'


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

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

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

else {
    console.log(i)
}


#2

In your second else if statement, you suddenly use the var fizzBuzz instead of i. But that is not the problem here.
The second else if statement will never be executed because of it's positioning.

Your second else if statement is supposed to execute when i % 5 === 0 and i % 3 === 0, but if i % 5 === 0 is true, the (else) if statement above has already executed.


#3

What do you think should I do now...?
How do I overcome?


#4

What is your problem? And could you share your code with us?


#5

Hey @uchihaaitachi,
I got the same problem earlier, and you just need to firstly analyze the && case, then the others : (and don't forget the {} in the "for" loop!)

for(var i = 1; i <= 20; i++) {

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);
}
}


#6

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