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 trialPaul Jackson
8,943 PointsDefining variable instead of passing argument?
Why are we defining the upper limit of the random number generating function as a variable rather than just passing the number 10000 as a parameter argument like we did when we were creating random numbers between 1 and 6?
1 Answer
Iain Simmons
Treehouse Moderator 32,305 PointsOkay so you might want to watch the video again, because Dave explains the reasoning behind why he coded this script the way he did.
First, the variable upper
was defined so that it could be easily changed right at the top of the script, and is a common practice in programming. It makes things more flexible. If this was a script with many more 'moving parts', so to speak, then it would take much longer to track down where the upper limit was being set. Also, by using an appropriately named variable, you make it much clearer what the variable is for and how it is used in the function.
Second, the randomNumber
variable is set because it is used more than once (first to compare each loop to the computer's guess
, and second to display to the user by adding to the HTML with document.write
). If you didn't set it as a variable, you would get a different value when displaying to the user because you would be running the function getRandomNumber
again. It probably could be named differently to make it more clear, like chosenNumber
or something, as it can be confusing when variables have similar names to the functions that their values are assigned with.
Third, if we set guess = randomNumber
, then we would essentially be 'telling' the compute the answer. and the loop would only run the first time and then exit, and we would be told it took 1 attempts to get it right. Try it yourself! The code guess = getRandomNumber(upper)
is telling the program to get another random number within the same range (i.e. using the same upper value), which it will then use for the next iteration of the loop.
The guess !== randomNumber
is a conditional check to see if guess
does not equal (or is not of the same type as) randomNumber
. Again, this might be confusing because of the names of the variables, but it's important to note that a single equals sign is used to assign a value to a variable, and two or three equals signs are used to check the equality of the expressions on either side.
Let me know if any of that doesn't make sense and I'll try and clarify further.
Paul Jackson
8,943 PointsPaul Jackson
8,943 PointsAnd then we also define a variable and put the function in it? Must we use a variable in the while loop? Couldn't we just call the function in the loop and then add what is returned to guess?
Furthermore, we set the while loop condition as guess !== randomNumber, but then we're assigning the function getRandomNumber( upper ) to guess (in the loop)? Couldn't we just assign var randNum to guess?