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

cp88
5,039 PointsPlease can someone review my Fizz Buzz code?
Hello
I have just completed the Extra Credit task at the end of "Control Structures", which is part of the "Introduction to Programming" course.
The task is the following:
Extra Credit: 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.
Hint. Use loops and if/else statments. In javascript the % is the modulo, or remainder operator. a % b evaluates to the remainder of a divided by b. 11 % 3 is equal to 2.
And this is my code:
var counter = 1;
while (counter <= 100) {
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);
counter = counter + 1;
}
Thank you!
9 Answers

cp88
5,039 Pointsvar counter = 1;
while (counter <= 100) {
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);
counter = counter + 1;
}

Erik McClintock
45,783 PointsCarrie,
Aside from the "counter = counter + 1" adding 101 to your count, your code is perfect! If you change the increment to "counter++", it works correctly and stops printing at 100. I'm not entirely sure why this is happening, but that seems to be the culprit. Perhaps someone else will jump on and provide further explanation for that cause, but in any case, if you switch that little part out, you're good to go.
Good work!
Erik

cp88
5,039 PointsHi Erik!
Thanks for coming back to me so quickly. When I run the code it says:
... 97 98 Fizz (99) Buzz (100)
Is this not correct?
Also, how do I post the code so it has the black background? :(
Thanks again :)

Erik McClintock
45,783 PointsCarrie,
If it is stopping at that last "Buzz" for you, rather than also then putting out "101", then you're good to go! When I copied and pasted your code into the console, it printed "101" after the final "Buzz", but if you're not seeing that, then no worries, I suppose =p
As far as posting code into the forums, this and this are handy posts to take a look at for help. But, basically, to post code blocks with that black background that you're seeing, you simply type three backticks (the [ ` ] character found usually in the top-left of your keyboard, perhaps directly to the left of your 1 key. It usually also has the tilde [ ~ ] symbol on its key), then type the name of the language you're going to be coding in (HTML, CSS, JavaScript, PHP, etc.), then hit enter and type another three backticks, then in between those two lines, you type your code.
Erik

cp88
5,039 PointsHi Erik
Not sure why you were getting to 101, very strange! Thanks for the tips about posting the code properly, looks to have worked :)

Erik McClintock
45,783 PointsGreat! Glad to have helped :)
Happy coding, and good work again on that FizzBuzz challenge! That's a coding question used frequently as an initial test when looking for potential developers, so feel proud that you nailed it!
Erik

cp88
5,039 PointsWow really?? Thanks Erik! :D

James Barnett
39,199 PointsYour code is solid, I would suggest you work on your code formatting to increase your readability.
For instance this is more readable so compare and contrast the whitespace in the 2 programs:
var counter = 1;
while (counter <= 100) {
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);
counter = counter + 1;
}
2 small nitpicks:
You can use
counter++
rather thancounter = counter + 1
which I think makes your code look cleaner but that's really a matter of style.Also you probably should use
===
to compare rather than==
I don't think the difference has yet been where you are in the course.

Joel Fredin
Courses Plus Student 1,971 PointsHello, I am new to to programming and wondering some stuffs according this assignment. But first, I will post my own code here for this assignment. The structure looks kinda similar
for (var counter = 100; counter; counter = counter - 1) {
if(counter % 15 == 0) {
console.log("fizzbuzz");
} else if (counter % 3 == 0) {
console.log("Fizz");
} else if (counter % 5 == 0) {
console.log ("Buzz");
} else console.log(counter);
}
console.log(x);
- I wonder if it is a bad idea to use the "for-loop". Are there cases where you prefer to use for loops instead of while loops and vice-versa?
- Are there any benefits to use incrementing instead of decrementing?
- Do you prefer that i use counter % 3 == 0 && counter % 5 == 0 instead of counter % 15 == 0? Since it means the same thing, but the code maybe looks better if you type "counter % 3 == 0 && counter % 5 == 0"? :)
Sorry for my (probably) silly questions but i am new to this and really wants to learn it :)
Thanks.

James Barnett
39,199 Points- I think a
for
loop is actually a little easier to understand because the decrementing (or incrementing as the case may be) is setup at the top of the loop - I incrementing is a little easier to follow, but it's mostly a choice of style
- I think
counter % 3 == 0 && counter % 5 == 0
is much more readable
There is a problem with your code though, it's an endless loop.
I'd add, I think it's more readable if you put your else / else if statements on their own line

Joel Fredin
Courses Plus Student 1,971 PointsThank you james, very straight forward and good answers :D