Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.