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
Gavin Broekema
Full Stack JavaScript Techdegree Student 22,443 PointsVariable Name vs. Parameters vs. Argument
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 to guess the correct number.</p>');
During this exercise I tried changing "var upper" to "var top" (along with the other necessary "upper" arguments) just to see how it would affect the program's functionality. The result was a broken program, which confused me.
Isn't the above function's parameter just a placeholder? If this is the case, why does the name of the variable in the argument matter when calling it? Does the variable name need to stay consistent with the parameter name for some reason? Where did I go wrong?
2 Answers
Shane Oliver
19,977 PointsIn the function you are passing an argument used to return a random number. This parameter name 'upper' must be used in the math random function.
When you call the function in the while loop you are passing the integer value stores in the variable at the top of the program var upper. To change the variable name to top you would change var upper to var top and in the while loop change guess = getRandomNumber (upper); to guess = getRandomNumber (top); to reflect the change in variable name.
It's is just coincidence that the var upper and the parameter upper have the same name, but they are not related.
Shane Oliver
19,977 PointsTop isn't a reserved word for JavaScript, but it is a reserved word in Windows. Here is a reference for the reserved words http://www.w3schools.com/js/js_reserved.asp
If you were using a windows based program, it is possible that the var name top broke your program - nice find
Gavin Broekema
Full Stack JavaScript Techdegree Student 22,443 PointsGavin Broekema
Full Stack JavaScript Techdegree Student 22,443 PointsThank you. I think the actual problem was that I used the variable name "top". Evidently the name "top" has some other semantic meaning in JavaScript, so when I used "top" the program broke. Every other variable name (along with changing the necessary arguments) worked fine.