Question 3 ...and the good(objects)


#1

can someone please tell me whats wrong with this?
Thanks in advance

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


#2

You're missing a last 'else' statement that will print out the numbers that aren't divisible by 3 or 5.

At least I think that's what your'e missing.


#3

Also, you cannot have a condition in your else statement.

Should be:

else {

#4

Thanks what do you mean than you cannot have a condition in an else statement
do you mean we can only have conditions in if and else if statements?


#5

Yes, an else statement only executes if no condition is met, so it doesn't need a condition.


#6

you have to put the last else statement first, at least that what worked for me


#7

I did the corrections and this is what I came out with! it said that I printed fizz when I should have printed FizzBuzz

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


#8

Try putting the && condition first. This is because the program is going to run through the first "if" statement first. If the first condition is "if (number % 3 === 0){console.log("Fizz");}", and 15 is divisible by 3, then the first condition is met and it will print out "Fizz."

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


#9

sequence is very much important here
the sequence is -
1. && case
2. %5,
3. %3,
here 2 and 3 can be altered but 1 should be at first, otherwise if you reverse the order given above in place of fizzbuzz it will print out fizz only because 15 is also divisible by 3 too.
but to make it printout fizzbuzz the && condition should be checked first.


#10

var i = 1;
for (i = 1; i<21; 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);
};
}