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 trialIosif Skorohodov
1,126 PointsIs 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
Treehouse TeacherHi 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!