Introduction to Objects 1


#1

Please tell me what is wrong with my code, it says Oops, try again. You printed FizzBuzz when you should have printed 1. These are the rules:
The rules:
For numbers divisible by 3, print out "Fizz".
For numbers divisible by 5, print out "Buzz".
For numbers divisible by both 3 and 5, print out "FizzBuzz" in the console.
Otherwise, just print out the number.
This is my code:

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

}
else if(i%5){
    console.log("Buzz");
}

else{
console.log(i);
}

}


#2

Hey @usarowing! Here are your problems:


#3

@amanuel2 What are my problems?


#4

Never mind i was about to edit anwser by giving question but seems like you dont have any problems. Probably a bug.


#5

@amanuel2, I'm not sure you finished your post prior to adding it as a reply.

@usarowing, Your logic was spot on, you have it exactly as it supposed to be. Well all except the conditions in your if statements.

if(i%5&& i%3){

Conditions evaluate if an expression is true or false, so somewhere in there you have to have a comparative operator

>
<
==

>=
<=
===

Try to add those in some way to your conditions, you're so close that I'm sure you can figure it out!


#6

Hey @usarowing

I have had the same problem before, and in my opinion you should try making it the divisbility of 15 rather than making it 3 & 5. The code should somewhat be like this:

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


#7

@xavier351, Technically you're right, however usarowing's argument also isn't incorrect. It's best to practice utilizing two conditionals in this lesson.

So using:

if(i%5===0 && i%3===0){

would work the same as:

if(i%15==0){

#8

@lolman

I see your point, but for me personally, it worked when I used 15, and it didn't when I used 3 && 5. I was suggesting that this MIGHT help, but you are totally right! It doesn't matter at all.


#9

@lolman thank you so much, that really helped! it was quite an easy fix!