FizzBuzz Exercise

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>
https://www.codecademy.com/courses/spencer-sandbox/0/3?curriculum_id=506324b3a7dffd00020bf661#

<In what way does your code behave incorrectly? Include ALL error messages.>
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”);
}
};

<do not remove the three backticks above>
1 Like

else is not allowed to have a condition
@obadahader

2 Likes

You have a condition for else… else doesn’t take a condition… You need to remove the condition

2 Likes

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

2 Likes

show me your code

@obadahader

1 Like

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:

1 Like

remove else statement

@obadahader

2 Likes

remove" ; "
i++; <=this

@obadahader

2 Likes

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

2 Likes

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

@obadahader

2 Likes

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 ?

2 Likes

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

2 Likes

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

1 Like

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

4 Likes

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

3 Likes

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

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

This thread has a great explanation:

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

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

3 Likes