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
Anthony Hill
Courses Plus Student 4,567 PointsFizzBuzz (extra credit: introduction to programming) - help needed...
Hi everyone,
I'm hoping someone can tell me where I'm going wrong - I've got the FizzBuzz program counting up using the 'for' loop but the divisible 'if' functions don't appear to be working, and it's just looping endlessly because I can't figure out how to end it at 100.
Made a bit of a mess but don't want to just 'give up' so any help would be appreciated.
for (var number=1;number; number = number +1) {
console.log(number);
number = number +1;
if (number % 3 == 0 && number % 5 == 0) {
console.log("fizzbuzz");
}
if (number % 5 ==0) {
console.log("fizz");
}
if (number % 3 == 0) {
console.log("buzz");
} else {
console.log(number);
}
}
2 Answers
Aleksandr Vinogradov
12,113 PointsYour loop is looping infinitely because you don't have a condition added to it. Here it will loop until number is less than 10
for (var number=1; number < 10; number +=1) {
console.log(number);
}
Aleksandr Vinogradov
12,113 PointsSorry for my typos :D
Brandon Barrette
20,485 PointsI fixed your typos for you. Note that in the lower right hand corner there are 3 dots, click that and you can edit your answer. This way future students who come across this post won't be confused with any typos.
Thanks for helping answer questions =)
Anthony Hill
Courses Plus Student 4,567 PointsAnthony Hill
Courses Plus Student 4,567 PointsThanks Aleksandr, that's fixed the whole thing and it works. Not sure why but I figured you couldn't put operators in the second part of the 'for' function :/