Lesson 3 - It looks like you printed out the wrong number of items


#1


Hello,

I've run this code, but I get an error message which says that I've printed out the wrong number of items. However I'm not sure how <= 20 created 21 and didn't return "Buzz"?

Any help would be great, thank you.


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

console.log(game);


#2

Remove the console.log(game); at the end of your code.


#3

Ahhhh okay, thank you :slight_smile:

I presume having the other console.log adds another game on top?


#4

No, adding another console.log(); doesn't increment game.

I'm pretty sure what's happening is that after the for loop is done (and game's value is 20), and all the code inside has ran, game is incremented one more time (to 21).


#5

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


#6

Hi all,

This is my code, and it worked fine...

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

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

}


#7

Hey,

Please help me find the mistake in here, 'cuz I'm not pretty sure:

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

The error is Oops, try again. You printed 3 when you should have printed Fizz :S


#8

It looks like you're missing another 'else if' for Fizz, you'll need to write another one like Buzz before your final else statement :slight_smile:


#9

Hi codenoobler,

Your code is absolutely correct. But you can spare some.

If you check the double equality in the first 'if', you'll not have to check the double condition '((i % x === 0) && (i % y != 0))' after that :wink:

Here's my code.

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


#10

not sure if you've received an answer yet, but your game is being initialized twice...
either "outsource" the var game without initializing (not recommended) or set the game type to var within your loop:
for (var game = ; game <= ; game)

hope this didn't confuse you further.


#11

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