3. ..And the good! Help


#1



For some odd reason, all values say either 'Fizz', 'Buzz', or 'FizzBuzz' even when the values aren't divisible by 3 or 5. What did I do wrong with the code?

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


Oops, try again. You printed Fizz when you should have printed 1


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


#2

This will never print FizzBuzz

Look at the steps you got the number evaluation in.

15 which would be FizzBuzz, is checked if its divisible by 3 first, which is is so it will return Fizz without even bothering to check if it's also divisible by 5

Also what is your FizzBuzz statement evaluating to? if i % 5 === ? && i % 3 === ? (replace the ? with a number) This is whats causing all numbers to print either Fizz or Buzz because i is divided by 5 and is then printing out Buzz because it doesn't need to equal anything to stop it. Maybe a 0 would be useful somewhere.


#3

HI and you should begin to count from 1 to 20


#4

Alright, after fixing up a few mistakes in the code, I was able to get it right. For some reason when you try re-ordering the statements they start working..

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

It's because you put === 0 for both these here that why its printing numbers instead of Fizz or Buzz or FizzBuzz because it was just thinking 1 divided by 3 is possible as 0.33 so thats Fizz, as you didn't state it needed to have no remainders.

and moving that means it checks if its divisible by 3 % 5 to give FizzBuzz first then moves on if it can't be divided by both


#6

Yeah, that was my mistake. For some reason I assumed that it would take 0 as true and would thus run FizzBuzz.


#7

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

what is wrong with this code? I'm not getting output.


#8

IDK if it's a glitch but you have to re-order it so that FizzBuzz is the first if statement


#9

Great Job mate!
after reordering that that works better.

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

}

#10

It's not a glitch.

If you have 15 which should return FizzBuzz, but are going through the if loop and the first thing it checks is if it's divisible by 3, it will return Fizz and move on to 16 without even checking if it's divisible by 5. Thats why we put divisible by 3 && 5 first because it checks them both before before singularly trying each number.

The loop checks the number, if it's divisible by the number it's being divided by with 0 left over it will return that statement, then it will move on, it doesn't bother to check if it's divisible by the other number because you haven't told it to.


#11

That explains it a lot better.. so in other words, when placing if statements, the one that evaluates the most goes in front. Alright, thanks!


#12

Also you could use switch case

for(var i=1; i<21; i++){
if(i % 3 == 0 && i % 5 == 0){
console.log('FizzBuzz');
}
else{
switch(true){
case (i%3==0):
console.log('Fizz');
break;
case (i%5==0):
console.log('Buzz');
break;
default:
console.log(i)
}
}
}


#13

Never thought of that!


#14

Hello everyone. Why is the following loop not valid?

for(var i = 0; i<20; i++) This will print 'FizzBuzz' instead of the number 1.
I know 1 % 3 === 0 is not true but shouldn't this loop go through all the numbers instead of starting at i = 1?

Thanks in advance for the explanation.


#15

Hi how do I get this code to work?

var divThree = i%3;
var divFive = i%5;
var divThreeFive = i%3 && i%5;

for(i=1; i < 21; i++) {
    if(divThree) {
        console.log(divThree);
        console.log("Fizz");
    } else if(divFive) {
        console.log(divFive);
        console.log("Buzz");
    } else if (divThreeFive) {
         console.log(divThreeFive);
         console.log("FizzBuzz");  
    } else {
         console.log(i);   
    } 
}

#16

Post your code please


#17

I'll give you the basic structure to this and you fill in the rest, you dont need the var's at the start although I do like the way you have attempted it.

for(var i = 0; i < 21; i++){
   if (       &&      ) {
 console.log("");
}else if (      ) {
console.log("");
}else if (      ) {
console.log("");
}else{ 
console.log(i);
}
};

#18

I wonder why too...maybe coz it's the longest statement.


#19

Thanks for your help @benjnev here's my code:

var game = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
for (var i = 1; i <21; 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);
}
};