FizzBuzz Exercise


#1



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


i dont know if am right with this code or not .. but i think that it makes sence .. when i press submit he tell me unexpected token {

what is the problem ^^


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


#2

else is not allowed to have a condition
@obadahader


#3

You have a condition for else.. else doesn't take a condition.. You need to remove the condition


#4

i thought that this is the problem .. i edited it but still the same error ><
@zainabrawat
@rcodeman


#5

show me your code

@obadahader


#6

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

:smiley:


#7

remove else statement

@obadahader


#9

remove" ; "
i++; <=this

@obadahader


#10

still the same .. and if i deleted the else statment .. with what should i replace it ?


#11

nothing
if you dont need else statement dnt use it
its not compulsory and show me your code

@obadahader


#12

thats my code .. and am trying to solve it :smiley:

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

i dont know i think the problem is with my else if use .. should i use this form or what ?


#13

your condition (i % 3 && 5 === 0) is not good, I think you mean (i % 3 === 0 && i % 5 === 0)


#14

Bonus Q @obadahader: can you think of a shorter way to check the common multiple of 3 and 5?


#15

This is my code, and works perfectly
I think u should start with if and the condiction &&

then use the simple if / else statements

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


#16

@philippemad is there a reason you think the "FizzBuzz" condition should come first?


#18

there seems to be something wrong with editor now because when I put the correct answer(s) in, it's
saying:
Make sure you are outputting JUST the numbers 1 - 20 without commas, spaces etc..

This is my code:

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

#19

Good question actually, I'm thinking something to do with the lexical scoping of JavaScript?
if we use a closure, it wouldn't matter.


#21

This thread has a great explanation:


#22

Try that mine has a small problem. It's logging twice instead of once. Otherwise it works.

var numbers = function(){
var caseClear = function(){
var num5 = i % 5 === 0;
var num3 = i % 3 === 0;

    if (num3 && num5 === true){
         console.log("FizzBuzz " + i);
    }else{
       //nothing to execute
    };
};



for (var i = 1; i < 21; i++){
     if (i % 3 === 0){
         console.log("Fizz " + i);
         caseClear();
     }else if (i % 5 === 0){
         console.log("Buzz " + i);  
         caseClear();
     }else{
         //Nothing to execute
         console.log(i);
     }
};

};

numbers();


#23

this works:

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