3... And the good!


#1



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


Why isn't this code accepted? It seems to print the right strings. I tried adding "console.log(i);" in the for brackets to print the number strings, but I get the same error.

Error Message: "Oops, try again. It looks like you printed out the wrong number of items."


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


#2
for (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%5 === 0 && i%3 === 0) {
            console.log("FizzBuzz");
        }
        else {
           console.log(i); 
        }
    }

#3

Check the instructions.

The rules:

  • For numbers divisible by 3, print out "Fizz".
  • For numbers divisible by 5, print out "Buzz".
  • For numbers divisible by both 3 and 5, print out "FizzBuzz" in the console.
    Otherwise, just print out the number.

Your loop for i = 15 will print:

Fizz
Buzz
FizzBuzz

but it should print only FizzBuzz.

Also, you aren't printing only number, when i isn't divisible by 3 or 5.


#4

It checks all your if statements together and will print out true for number that are divisible by both 3 and 5 .
what you can do is give the highest preference to the numbers that are divisible by both 5 and 3 .

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


#5

why the syntax is i%3==0 and not i%3===0


#6

Can you explain why yours works when

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

doesn't? What does the !== do in the if & else if?


#7

Your's isn't working because you need to check if a number is divisible by 3 and 5 first.

Because once it gets to 15 it will see check its divisible by 3 which is it then move on to 16 without seeing if it's divisible by 5.

His method is overly complicated including ! which is like an opposite so: !true = false and !false = true.


#8

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