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
Jordan Hammond
2,278 PointsWrite a program that loops through the numbers 1 through 100. Each number should be printed to the console.
Can someone review my code and tell me why it won't work. I know how to get the right answer after looking at other posts but I still don't understand why this won't work.
It's from this extra credit question: 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!
var counter=100
while(counter) {
if(counter%15=0){
console.log("fizzbuzz")
counter=counter-1
} else {
if(counter%3=0){
console.log("fizz")
counter=counter-1
} else {
if(counter%5=0) {
console.log("buzz")
counter=counter-1
} else {
console.log("number " +counter)
counter=counter-1
}
}
}
}
3 Answers
Ben Stoltz
20,364 PointsAll of your modulo (%) statements are only using one =. == will work but it might be a good habit to get into with using ===.
== : Will compare both values, but will convert type. So '2' == 2 will return True.
===: Strict comparison '2' === 2 will return False.
It's just a good habit to get into.
Bassem Al Hassouni
Courses Plus Student 3,395 Pointscan it be done like this
for (var i = 0; i < 100; i += 1) {
if (i % 15 === 0) {
console.log("fizzbuzz");
} else if (i % 3 === 0) {
console.log("fizz");
} else if (i % 5 === 0) {
console.log("buzz");
} else if (i % 3 > 0 || i % 5 > 0) {
console.log(i);
}
}
Marisa Hendrickson
8,303 PointsYou can also do it this way
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);
}
}
Jordan Hammond
2,278 PointsJordan Hammond
2,278 PointsNevermind. Figured it out.
my
counter%3=0needs an extra =
so it should look like this:
counter%3==0