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?

randnum/ randomNumber......????

Firstly my code runs fine )and i understand it about 99%. However i don't understand the variable part randomNum and why randomNumber has an a value of (6). Or isit just to set a value in order for the loop to function

Thanks in advance

function randomNumber(upper) { return Math.floor( Math.random() * upper ) + 1; } var counter = 0; while(counter <1000 ){ var randomNum = randomNumber(6); document.write(randomNum + " "); counter+=1; }

1 Answer

Adrian Patrascu
Adrian Patrascu
15,279 Points

Hi Ben,

A very good question. From what I can see the loop runs 1000 times because of the counter you have setup to be <1000. Regarding the randomNumber function I believe 6 has been the number chosen in order to replicate the throwing of a dice which has 6 numbers as well.

I hope this helps. You can chose whatever number you want and the while loop will generate 1000 random numbers from 1 to the number specified in the function when you call it.

Thank you, Adrian

Cheers Adrian!! Great info!!