Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

Fizzbuzz challenge

I am currently stuck on the Fizz Buzz challenge, when i go to run the script i get an unexpected token error. I have a feeling that the issue is in my formatting and i am on the right track for how to actually structure out the code. I am not looking for an answer just to be pointed in the right direction.

for (var counter =1; counter < 101; counter=counter+1) { console.log(counter) if (counter%3==0 && counter%5==0) { console.log("fizzbuzz"); } else (counter%3==0) { console.log("fizz"); } else(counter%5==0){ console.log("buzz"); } else { console.log(counter); }

6 Answers

Cassio Victor Cadore
Cassio Victor Cadore
6,579 Points

Yes Andrew, there is a semicolon missing in the first console.log(counter). Did you forget it, or just don't think it should be there? Here you have some info about semicolons and Javascript: http://www.codecademy.com/blog/78-your-guide-to-semicolons-in-javascript

Thank you very much for your help, i was not aware a semi colon needed to be there. Thank you for the link, that read will be a huge help.

Cassio Victor Cadore
Cassio Victor Cadore
6,579 Points

Hey Andrew!

I'll try to help you wiyhout giving you the answer... Check how the if...else statement is used! And if all closures {} are correct!

David Curtis
David Curtis
11,301 Points

check your usage of if and else

alright, i changed my else statements to else if. Now i think i am down to some misplaced semi colons or brackets, the new error i get when running the script is unexpected end of input

for (var counter =1; counter < 101; counter=counter+1) { console.log(counter) if (counter%3==0 && counter%5==0) { console.log("fizzbuzz") } else if(counter%3==0) { console.log("fizz") } else if(counter%5==0){ console.log("buzz") } else { console.log(counter) }

Cassio Victor Cadore
Cassio Victor Cadore
6,579 Points

Are you sure that you are closing all loops? For, if, else...

Added closing curly brace to the end of else statement, pretty sure i need two there, one to close the else statement and one to close the for loop. still getting the same error though, I am not sure i understand the proper placement of semi-colins here.

for (var counter =1; counter < 101; counter=counter+1) { console.log(counter) if (counter%3==0 && counter%5==0) { console.log("fizzbuzz"); } else if(counter%3==0) { console.log("fizz"); } else if(counter%5==0){ console.log("buzz"); } else { console.log(counter);} }