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

What Did I do wrong? (Introduction to Programming) Control Structures: Extra Credit Question Fizz Buzz

Hey all,

I am having trouble with my code in the extra credit question for Introduction to Programming: Control Structures. I seem to have something wrong with my syntax, token, or some other formatting error. Can anyone explain where are my mistakes and how to fix them?

//Fizz and Buzz code
for (var i = 100;i >= 1;i - 1) {
if (i % 3) {console.log ("Fizz");}
else if (i % 5) {console.log ("Buzz");}
else (i % 5 === 0 && i % 3 === 0) {console.log ("FizzBuzz");}
}

Just a heads up, or in case you need a reminder or need to know what the question was here it is: Fizz Buzz

Write a program that loops through the numbers 1 through 100. Each number should be printed to the console, using console.log(). However, if the number is a multiple of 3, don't print the number, instead print the word "fizz". If the number is a multiple of 5, print "buzz" instead of the number. If it is a multiple of 3 and a multiple of 5, print "fizzbuzz" instead of the number.

Thanks for any help I can get!

Alex

2 Answers

Your if statements are not checking against any value. E.g. 'if (i % 3)': this is not comparing the result of 'i % 3' to any other value, and since it will return an integer and not a boolean it is incorrect. Try something like if (i % 3) == 0 {"Do this..."}.

You need to also consider the order of your if statements. If i = 30, your code would print "Fizz" and end that iteration of the for loop. Try putting the FizzBuzz statement first.

Try something like this:

for(int i=0; i<=100; i++)
{
  if((i%3 == 0) && (i%5 == 0)) console.log("FizzBuzz");
  else if(i%3 == 0) console.log("Fizz");
  else if(i%5 == 0) console.log("Buzz");
}

To get you started...

  • The last "else" in your code should actually be an "else if", since you still have conditions associated with it.
  • I'm not completely sure why (someone more knowledgeable than I will have to fill you in on this), but you need to do "i--" rather than "i - 1" in your for loop. For some reason, the way you have it programmed, it doesn't decrement properly.

Here's is corrected code that runs:

//Fizz and Buzz code
for (var i = 100; i >= 1; i--) 
{
    if (i % 3) {console.log("Fizz");}
    else if (i % 5) {console.log("Buzz");}
    else if (i % 5 === 0 && i % 3 == 0) {console.log("FizzBuzz");}
}

I'm not sure if this gives you the exact answer, but it's a place to start from. It doesn't look like you are printing each number to the console, so I'd definitely look at that first.

EDIT: regarding "i - 1": Basically, that is an incomplete statement, since i is never updated -- you never set i to any value. You could write it like this:

i = i -1

or

i -= 1

Thanks, fixed those up and helped me get a start.