Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Extra Credit stage: Fizz Buzz - statement expected problem

I just ran into a problem while doing the extra credit stage and I can't figure out how to fix it. The code is as follows:

var Counter = 1;

while(Counter < 100){
    if(Counter % 3);{
        console.log("fizz");
    } //This is where the statement is expected.
else(Counter % 5);{
        console.log("buzz");
    } //This is where the statement is expected.
else (Counter % 3 % 5); {
        console.log("fizzbuzz");
    }
    Counter = Counter - 1;
}
Dave McFarland
Dave McFarland
Treehouse Teacher

Can you give us a link to the page containing the extra credit? Or just past the extra credit instructions in here?

2 Answers

Dave McFarland
STAFF
Dave McFarland
Treehouse Teacher

Hi Samuel Lundgren

I'm not sure what the extra credit assignment is asking you to do, but I can see a few mistakes in your code.

  1. Don't add a semicolon after a condition. For example this is wrong: if(Counter % 3);. It should just be if (Counter % 3)

  2. You use the else keyword as the LAST item in a series of conditional statements and it's used to provide a default action if the other conditional statements fail. You don't ever put a condition after else, so else (Counter % 5) is incorrect. You should use else if here which provides another condition for testing:

if (Counter % 3) {

} else if (Counter % 5) {

}
  1. You have what's called an "infinite loop" here -- that is, the program will NEVER stop. The while condition runs as long as Counter < 100. Since Counter starts with a value of 1 and each time through the loop you subtract 1 from that, Counter will ALWAYS be less than 100 and the program won't end. However, if you wanted the loop to run 100 times, you could do this:
var counter = 100; 
while (counter > 0) {
  // do stuff
  counter = counter - 1;
}

I know how to fix my problem now, thanks!

I know how to fix my problem now, thanks!