JavaScript JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops A Closer Look at Loop Conditions

Is the getRandomNumber a function of Javascript?

var upper = 10000;
var randomNumber = getRandomNumber ( upper ); var guess;
var attempts = 0;

function getRandomNumber(upper) {
return Math.floor( Math.random() * upper ) + 1; }

while ( guess !== randomNumber ) {
guess = getRandomNumber( upper ); attempts += 1; } document.write("<p>The random number was" + randomNumber + "</p>"); document.write("<p> It took the computer " + attempts + " attempts get it right.</p>");

2 Questions:

1 - In the while loop, is the getRandomNumber a condition in Javascript itself or is that from the function we created after the variables?

2 - Again in the while loop. is the ( upper ) after the guess = getRandomNumber considered a condition or something else? If so, could someone explain it?

1 Answer

Jennifer Nordell
Jennifer Nordell
Treehouse Staff

Hi there! The getRandomNumber function is your own custom defined function. On line 1 of your code you have var upper = 10000; The reference to getRandomNumber inside the while loop is known as "calling a function". We're saying "please run this segment of code". The upper in parentheses at the call site is called an argument. This is what we're sending into that function for it to work with. When this happens the function will now use the value in this line:

return Math.floor( Math.random() * upper ) + 1; }

This line will return a value between 1 and 10000. If you were to go back and change line 1 to var upper = 50, then that line would produce a random value between 1 and 50.

Hope this helps! :sparkles: