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

JavaScript

Fizz Buzz with value from user

I wanted to see if I can create the Fizz Buzz output based on a number entered by the user.

I can get it to work counting down:

var getNumber = prompt("How many numbers do you want to test?");

for (i = getNumber; i > 0; i--) {
    if (i%3==0 && i%5==0) {
        console.log(i, "fizzbuzz");
    } else if (i%3==0) {
        console.log(i, "fizz");
    } else if (i%5==0) {
        console.log(i,"fizz");
    } else{
        console.log(i);
    };
};

I'm can't get it to display the results in ascending order, and I'm sure I am missing something very simple.

Any help with be great. Thanks

2 Answers

Peter Szerzo
Peter Szerzo
22,661 Points

Abhijat,

Your are implementing a loop that does output in descending order, starting from getNumber and decreasing to zero. You want to move the other way:

for (var i = 1; i <= getNumber; i += 1) {
  // code as usual
}

Thanks, that did it.