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!

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 JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops What are Loops?

Aakash Srivastav
Aakash Srivastav
Full Stack JavaScript Techdegree Student 11,638 Points

while loop

Please review my code. I have tried a different approach . I give the independency to user to choose the "upper" limit. -

function getRandomNumber(upper){
    var counter = 0;
    while( counter < 100){
        randomNumber = Math.floor(Math.random() * upper + 1);
        document.write(randomNumber + " ");
        counter += 1;


Is there any limitation to this approach ?

Mathew Tran
Mathew Tran
Courses Plus Student 10,205 Points
  • Change the variable name, 'upper' to 'upperLimit'
  • Remove the space before counter on line 3
  • line 6 can be transformed into counter++

In terms of limitations, I'd say it depends what you are going to use this code for, it seems sufficient enough for this lesson :)


1 Answer

Steven Parker
Steven Parker
228,119 Points

I'm not sure what some of Matthew's comments are based on, in particular:

  • the parameter name is fine as is
  • your method of incrementing "counter" is already the recommended "best practice"

For pure cosmetics, removing the space in the "while" expression is good, but it would also be nice to add a space after "while" and also before the braces that start code blocks.

Functionally, the code is excellent, I can't think of any limitations. Good job! :+1: