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
Ali Amirazizi
16,087 PointsJS extra credit- What is wrong with my code?
Trying to complete the extra credit but running into some issues. I'm seeing the numbers for fizz and buzz display when they are not supposed too. However, the fizzbuzz numbers don't seem to have this issue.
"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."
My Code:
for (var counter = 100; counter; counter = counter -1) {
if (counter % 3 === 0) {
console.log("Fizz");
} else {
null
}
if (counter % 5 === 0) {
console.log("Buzz");
} else {
null
}
if (counter % 15 === 0) {
console.log("fizzbuzz");
} else {
console.log(counter)
}
}
1 Answer
Cosmin CioaclÄ
21,339 PointsHi Ali,
Let's first look at the for loop:
You seem to want to go from 100 to 0. If so, the second argument should be counter > 0, not just counter.
for (counter = 100; counter > 0; counter--) {}
Inside the for loop you want to have just one if/else statement, not 3. This should check from the most particular case to the most general case, which will end up on the else branch.
It should look something like this:
if (counter % 15 === 0) {
console.log("fizzbuzz");
} else if (counter % 3 === 0) {
console.log("Fizz");
} else if (counter % 5 === 0) {
console.log("Bizz");
} else {
console.log(counter);
}
Hope that helps, Cosmin
Ali Amirazizi
16,087 PointsAli Amirazizi
16,087 PointsHi Cosmin, thanks for the reply. This helps simplify things; especially with the "else if" statements. I tried it out, however, I was unable to make it run. I get this error:
"[Error] SyntaxError: Unexpected token ')' (anonymous function) (fizzbuzz.js, line 1)"
I tried resolving the issue by not wrapping the conditions in the loop, but that didn't work. Also I noticed you did not declare "counter" in your loop statement. Not sure if that's necessary or not. I tried removing the declaration keyword but it didn't make a difference.
Here's the code:
Ali Amirazizi
16,087 PointsAli Amirazizi
16,087 PointsI tinkered around with the loop statement and resolved the issue. This also sets the counter to count from 1-100 instead of 100-1, which is what I actually wanted. Thanks for all your help.
Cosmin CioaclÄ
21,339 PointsCosmin CioaclÄ
21,339 PointsHi Ali,
The error is in your for loop. For more details about how to write loops you can check out this link. You wrote counter== instead of counter-- (which is the same as counter = counter - 1).
This is the complete working code:
If you already have a variable declared before the for loop, you can use that and not declare a new one inside the for initialization. If you do declare a variable inside the for (var counter), keep in mind that it's scope is the same as the for loops scope.
Hope that helps.