13.3....and the good: Wrong number of items error


#1

Hi, I keep getting the wrong number of items error. I'm not sure why it is coming up. It prints fizz, buzz or fizzbuzz when it's supposed to and the regularly old number as well if not divisible by 3 or 5. I've used a while loop unlike what others have done, other than that I'm not sure. Any insight would be appreciated:

x = 1
while (x<20)
{
if (x%3===0)
{
if (x%5===0)
{
console.log("fizzbuzz")
}
else
{
console.log("fizz")
}
}
else if ( x%5===0)
{
console.log("buzz")
}
else
{
console.log(x)
}

x=x+1
}


#2

Print out the numbers from 1 - 20.

It means from 1 to 20 inclusive :slightly_smiling:


#3

Use **for** loop for this course:

for (CONDITION) {
If (CONDITION === True) {
Console.log(X);
} else if (CONDITION === True) {
Console.log(Y);
} else {
Console.log(Y);
}
}


#4

Hey there. I have another issue coming.

Here's the code:

var numbers = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];

var i = 0;

for(i = 1; i < 20; i++){

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

And it works almost fine, the only thing is that it logs 'Fizz' together with 'FizzBuzz', (first comes Fizz and the next line is FizzBuzz). What is says is "You printed Fizz when you should have printed FizzBuzz". I just can't find out what could be the reason for that...

Any hint would be appreciated.


#5

This loop is last why would a number check to see if it's divisible by 3 & 5 if it could already be divisible by 3.

So, 15 is the only number that will print FizzBuzz, but if it's checking first that its divisible by threee it will give Fizz then move on to 16 and check that.

Do you understand what I mean?


#6

I also have this problem. But after reading ur comment, I add the second condition to the first and second "if", and it works!Thanks a lot
Here is my code:
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)}}


#7

You could have had this first. Followed by this second, which could have been written easier

if (i%3 === 0)

meaning you wouldnt need the i%5 !==0


#8

This is my code. And it works.
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);
};
}


#9

&& you're missing a & symbol.