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

Aakash Srivastav
seal-mask
.a{fill-rule:evenodd;}techdegree
Aakash Srivastav
Full Stack JavaScript Techdegree Student 11,622 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;
    }
}

getRandomNumber(15);

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 :)

Matt

1 Answer

Steven Parker
Steven Parker
210,475 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: