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
Tiberiu Ion
14,811 PointsIn a for loop that counts from 1 to 100 I don't know how to replace the number 3 and 5 with "fizz" or "buzz".
I am trying to complete an extra credit that requires me to write a fizz buzz game using loops and if/else statments. As a hint, they mention the % modulo operator, that's why I'm using it. What am I doing wrong ?
for (i=1; i<101; i = i +1) {
console.log(i);
if (i%3==0){
console.log(i, "fizz");
} else if (i%5==0) {
console.log(i, "buzz");
}
}
2 Answers
Sreng Hong
15,083 PointsHi Tiberiu
It's good for you to try an extra credit from the course.
By the way, I think you've messed up with the control structures and I'm not sure if you know how to use the remainder operator (%) or not. I'm going to write you a right code, and if you still don't understand you can ask me.
for (var i = 1; i <= 100; i += 1) {
if (i % 3 == 0 && i % 5 == 0) {
console.log("fizzbuzz");
} else if (i % 3) {
console.log("fizz");
} else if (i % 5) {
console.log("buzz");
} else {
console.log(i);
}
}
Hope this helps.
Enmanuel Ruffinelli
8,407 PointsHi Tiberiu Ion,
I wrote the code, but I did it with the "while" function:
var counter = 100;
while (counter) {
if (counter % 15 == 0) {
console.log("fizz buzz");
} else if (counter % 5 == 0) {
console.log("buzz");
} else if (counter % 3 == 0) {
console.log("fizz");
} else {
console.log(counter)
}
counter = counter - 1;
}
I don't know if this is 100% correct, but it worked for me!