3. ...And the good!


#1


Hi, stuck on this. The error says 'Oops, try again. It looks like you printed out the wrong number of items.'


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


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


#2

It now says, after a few changes, the exact same thing. The screen thing says
Fizz
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
Fizz
16
17
Fizz
19
Buzz
Please help me! :cry:

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

#3

Your loop is printing numbers for 0-20, instead of 1-20.
In your for loop, change the value of i to 1 instead of 0.
Also, make Fizz and Buzz values only print when the value is divisible by its respective number and not by the other, as the fifteenth number of the loop only prints Fizz, not FizzBuzz.

Example that worked for me:

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

#4

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


This should also be correct. The thing I changed is that I check for the doubles first and then the remainder.


#5

Instead of doing i < 21, just do i <= 20. It's just more elegant that way.


#6

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