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 A Closer Look at Loop Conditions

Tyler Dix
Tyler Dix
14,230 Points

I've changed my upper variable for those who are confused about variable scope. Hope it helps...

Using the "upper" string as a variable, and also as a parameter for the function, confused me. To help clarify, I've changed my code. The program runs just fine, and helped me understand scoping a bit more clearly.

var varUpper = 10000; // note the changed variable
var randomNumber = getRandomNumber(varUpper); // also used here
var guess;
var attempts = 0;

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

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

4 Answers

Steven Parker
Steven Parker
229,644 Points

Note that the parameter (and previously variable) named "upper" represents a number value and not a string.

But you're quite right that changing the variable name has no effect on program operation. And it could be a "best practice" in actual code to help make it more readable. It also fixes a potential scope issue known as "shadowing", where the global variable cannot be accessed inside the function because the parameter's name conflicts with it.

Steven Parker
Steven Parker
229,644 Points

Tyler Dix — Did that help? You can mark a question solved by choosing a "best answer".
And happy coding!

Jiten Mehta
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Jiten Mehta
Front End Web Development Techdegree Graduate 21,209 Points

Thanks for this. I initially thought the variable upper and the parameter of upper in the function was somehow connected. Your program helped me understand. Cheers buddy!

Veselin Radosavljevic
Veselin Radosavljevic
3,863 Points

but i still dont get it what do we pass then in upper ,what value for the function getRandomNumber ?

Steven Parker
Steven Parker
229,644 Points

It picks a number at random, that's its purpose. The value it gives will be some number from zero up to the "upper" value.

Sandro Würmli
Sandro Würmli
15,353 Points

Because it's not a question, i'll add a common method for variable names. You should start the variable name with a short version of the type you'll put the variable in. For example, if it's a string, var sName = 'Max'; should be nice. If it's a number, let's say it's an integer, var iUpper = 10000; should be okay. Because you already now it's a variable, it might not be a good idea to start with var var… = 'something';. It'll just confuse you more than it should.

Hope that helped.