Unexpected Error FizzBuzz exercise


#1

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


for(i=1; i < 20; i++){
console.log(i);

if (i % 3 === 0 || i % 5 === 0){ 
console.log ("FizzBuzz"); 

} else if (i% 3===0) {
    console.log ("Fizz"); 
    
}else{ 
    console.log("Buzz");

}
};


#2


if (i % 3 === 0 || i % 5 === 0){ console.log ("FizzBuzz"); //do you really want it?

Short-Circuit Evaluation

As logical expressions are evaluated left to right, they are tested for possible "short-circuit" evaluation using the following rules:

false && (anything) is short-circuit evaluated to false.
true || (anything) is short-circuit evaluated to true.

check it
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators

make sense :slight_smile:


#4

Wasn't sure where to post this for the best response, so I hope adding it here is okay. I am getting the error "Unexpected Token )" ... I have tried finding a random ) but, I must be missing something because I can't figure out why it's causing me a problem.

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

#5

HAVE A CLOSE LOOK AT

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

AND

REMOVE LAST ONE } BRACKET


#6

Ahh! I kept looking right over that comma! Thank you!!


#7

This solution works. Check it out if you want to compare :slight_smile:

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

sorter()

#8

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