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 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!!