3 - Introductions to Objects


#1

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

Why doesn't this code work? :man:


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

See the difference?
1st : i added else before other consecutive if statements.
2nd : in ur code, it does not reach till checking (i%3 && i%5) because if it is divisible by both, the 1st if statement is also true and it stops there. so u will get error for not printing "FizzBuzz"


btw, this is my code which i used, if u notice, i removed checking if its is divisible by 3 or 5:

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