FizzBuzz logs out undefined as last item


#1


https://www.codecademy.com/courses/spencer-sandbox/0/3?curriculum_id=506324b3a7dffd00020bf661


I'm almost 100% sure this is the right code, but for some reason whatever I try to do, it will still log out the last item as "undefined".
This is what the console logs with my code:
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
undefined

Any help is greatly appreciated :slight_smile:


function fizzBuzz(){
	for(var i=1;i<=19;i++){
		if(i%5 === 0 && i%3 === 0){
			console.log('FizzBuzz');
		} else if(i%3 === 0){
			console.log('Fizz');
		} else if(i%5 === 0){
			console.log('Buzz');
		} else {
			console.log(i);
		}
	}
}

console.log(fizzBuzz());


#2

Should be listing 20 items, not 19.

The SCT (lesson checker) does not expect a function so will not be able to call this. Just run a for loop. The undefined is caused by the function not having a return value. It will go away after you fix the code.


#3

Thanks! Getting rid of the function made it work. When inside the function the SCT actually didn't accept i <=20 and I had to change it down to 19. Without the function it's 20, yep :slight_smile:
Thanks again!


#4

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