# And the good...undefined getting printed in terminal

#1

So i tried to run the below code and there is an undefined printed in it. And it says "Looks like you have printed out the wrong number of items." Please help.

``````var numbers = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
var Fizzbuzz= function(){

for ( var i=0; i < numbers.length; i++){
if (numbers[i] % 3 === 0 && numbers[i] % 5 ===0){
console.log("FizzBuzz")
}
else if (numbers[i] % 3 ===0) {
console.log("Fizz")
}
else if(numbers[i] % 5 ===0) {
console.log("Buzz")
}
}
};
console.log(Fizzbuzz());

And my console prints this::
Fizz
Buzz
Fizz
Fizz
Buzz
Fizz
FizzBuzz
Fizz
Buzz
undefined``````

#2

undefined is simply there because your function doesn't return anything

when the number is neither divisible by 3 or 5 (or both) you should log the number to the console, you don't do this

#3

Ok...I did this and the output is still not working.

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

``````for ( var i=0; i < numbers.length; i++){
if (numbers[i] % 3 === 0 && numbers[i] % 5 ===0){
console.log("FizzBuzz")``````

}
else if (numbers[i] % 3 ===0) {
console.log("Fizz")
}
else if(numbers[i] % 5 ===0) {
console.log("Buzz")
}
else {
console.log([i])
}
}
};
console.log(Fizzbuzz());

output-----

[ 0 ]
[ 1 ]
Fizz
[ 3 ]
Buzz
Fizz
[ 6 ]
[ 7 ]
Fizz
Buzz
[ 10 ]
Fizz
[ 12 ]
[ 13 ]
FizzBuzz
[ 15 ]
[ 16 ]
Fizz
[ 18 ]
Buzz
undefined

#4

0 should not be included? 1 till 20 (1 and 20 included)

also, the numbers should be integers, not integers inside an array

#5

I'm still not getting it. I don't understand when you meant integers shouldn't be in an array. And why should i also log other numbers when they didnt ask me for it?

#6

integer:

``1``

integer in an array:

``[1]``

you shouldn't? you should do the numbers asked for by the exercise (1 till 20, 1 and 20 included) but in your case i also see zero, the exercise doesn't ask for zero

#7

Okay,,

I did that and this is what I'm Getting.
var numbers = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
var Fizzbuzz= function(){

``````for ( var i=1; i < numbers.length; i++){
if (numbers[i] % 3 === 0 && numbers[i] % 5 ===0){
console.log("FizzBuzz")``````

}
else if (numbers[i] % 3 ===0) {
console.log("Fizz")
}
else if(numbers[i] % 5 ===0) {
console.log("Buzz")
}
else {
console.log(i);
}
}
};
console.log(Fizzbuzz());

Output---
1
Fizz
3
Buzz
Fizz
6
7
Fizz
Buzz
10
Fizz
12
13
FizzBuzz
15
16
Fizz
18
Buzz
undefined

#8

lets go back to this code:

``````var numbers = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
var Fizzbuzz= function(){

for ( var i=0; i < numbers.length; i++){
if (numbers[i] % 3 === 0 && numbers[i] % 5 ===0){
console.log("FizzBuzz")

}
else if (numbers[i] % 3 ===0) {
console.log("Fizz")
}
else if(numbers[i] % 5 ===0) {
console.log("Buzz")
}
else {
console.log([i])
}
}
};
console.log(Fizzbuzz());``````

and then in particular here:

``````else {
console.log([i])
}``````

`i` is the index, so if you want the number you need `numbers[i]`

#9

Did that earlier...same issue. Printed the wrong number of items....It's really getting on to me now

#10

because of this line:

``console.log(Fizzbuzz());``

you also log what is returned, this results in an additional item being logged, thus the wrong numbers of items is logged

#11

Everything is correct now, but still says wrong number of items printed...

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

``````for ( var i=[0]; i < numbers.length; i++){
if (numbers[i] % 3 === 0 && numbers[i] % 5 ===0){
console.log("FizzBuzz")``````

}
else if (numbers[i] % 3 ===0) {
console.log("Fizz")
}
else if(numbers[i] % 5 ===0) {
console.log("Buzz")
}
else {
console.log(numbers[i]);
}
}
};
console.log(Fizzbuzz());

output --------------------
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz
undefined

#12

remove the console.log at the function call, this will log undefined to the console, resulting in the 21st item, causing an error

#13

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