3...and the good


#1


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

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


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


#2

Switch the most restrictive condition to first position :
for(i=1; i<21; i++) {
if((var 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);
};
}


#3

you code doesn't work for me :SyntaxError: Unexpected token var


#4

Here is your posted code:

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

According to your code, you're testing the following:
If i is divisible by 3, print Fizz.
If it's not divisible by 3, but it's divisible by 5, print Buzz.
The following will never happen, because if i is divisible by either 5 or 3, it will never reach this line:
If i is divisible by 5 and i is divisible by 3, print FizzBuzz.

Try the following instead:

for(var i=1;i<21;i++){
    /*place your last test as the first*/
    if((i%5===0) && (i%3===0)){
        console.log("FizzBuzz");
    }else if(i%3===0){
        console.log("Fizz");
        
    }else if((i%5===0)){
        console.log("Buzz");
        
    }else{
        console.log(i);}}

Of course, there are other ways to do this, but that's how you'd go about fixing your code.


#5

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