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
Asia Chan
11,007 PointsRandom Number Challenge: When is the right time to use parameters in a function?
My original code included parameters, didn't work:
var upper = prompt("We need two values for a random number. What is your upper value?");
var lower = prompt("Now, what is your lower value?");
var getRandomNumber = function(upper, lower) { //included parameters
upper = parseInt(upper);
lower = parseInt(lower);
var randomNumber = Math.floor(Math.random()*upper-1+1)+lower;
return randomNumber;
}
document.write("Your random number is " + getRandomNumber() + ".");
When I removed the parameters, the code worked:
var upper = prompt("We need two values for a random number. What is your upper value?");
var lower = prompt("Now, what is your lower value?");
var getRandomNumber = function() { //removed parameters
upper = parseInt(upper);
lower = parseInt(lower);
var randomNumber = Math.floor(Math.random()*upper-1+1)+lower;
return randomNumber;
}
document.write("Your random number is " + getRandomNumber() + ".");
I mainly discovered this via trial & error. That's pretty counter-productive for me, so when exactly would you know when to use parameters in a function?
2 Answers
Codey Novice
Full Stack JavaScript Techdegree Student 2,022 PointsHey AsiaChan,
Is you code running properly? I copied and pasted your new code into an IDE and it is picking up an error for your last quotation mark in the line with "document.write...". I am not sure what character you typed in (bc it looks just like a quotation mark), but I changed it to a standard double quotation mark just like you have before the word "Your" and after the word "is" and the error went away. Other than that, I believe your code should run properly. If it does not, let me know, and I'll try to run it all the way.
To clean up your code and use the DRY principle, you could use parseInt with the prompt method like below: var upperCall = parseInt(prompt("Enter your max number"));
Also, I don't believe your random number generator will ever produce the minimum number bc of the parenthetical grouping. Try random = Math.floor(Math.random() * (max - min +1) + min);
Let me know...
Codey Novice
Full Stack JavaScript Techdegree Student 2,022 PointsWhen a function has parameter variables, the names of these parameters will override any global variables of the SAME name within the scope of that particular function. So, when the parameters "upper" and "lower" were added, these variable names overrode the global variables of the same name that were created previously. However, when the function "getRandomNumber()" is called without arguments, as is the case in the line of code beginning with "document.write...", the parameters "upper" and "lower" will have the value of "undefined". If only one argument is passed, the first parameter will be assigned the value of that argument and the second parameter will be undefined and so forth.
In short, if getRandomNumber is called with the proper arguments, in this case getRandomNumber(upper, lower), the code with the function parameters should work because the function is called from outside of the function scope. The function scope is where the function is defined. When you are outside of the function scope, the variables "upper" and "lower" will refer to the global variables created at the top of the code which is outside of the function scope.
To further clarify, the name of the global variables could (but it is NOT necessary) be change to upper1 and lower1. Then, the name of the function parameters could remain as they are (i.e. "upper" and "lower"). Finally, from outside of the function definition, the function could be called as "getRandomNumber(upper1, lower1)" which will pass the values of "upper1" and "lower1" to the parameters "upper" and "lower".
Hope this helps.
Asia Chan
11,007 PointsHi Codey, thanks for your answer.
I've revised my code, would this be the more preferable answer? Thanks!
var upperCall = prompt("We need two values for a random number. What is your higher number value?");
var lowerCall = prompt("Now, what is your lower number value?");
var getRandomNumber = function(lower, upper) { // works with parameters
var upper = parseInt(upperCall);
var lower = parseInt(lowerCall);
var random = Math.floor(Math.random()*(upper-lower)+1)+lower;
return random;
}
document.write("Your random number is " + getRandomNumber() + ".");
Asia Chan
11,007 PointsAsia Chan
11,007 PointsHi Codey,
What does DRY stand for? Yeah I was thinking of compressing the code but for clearer presentation I opted to have separate variables instead. Regarding the parenthetical grouping, I'll keep that in mind. I think the quotation marks was due to a space I deleted earlier. I think I forgot to change them into beginning and end quotes. I thought that should be automatic in Treehouse's workspace. Changed it nonetheless. ?
Thanks for the input!
Codey Novice
Full Stack JavaScript Techdegree Student 2,022 PointsCodey Novice
Full Stack JavaScript Techdegree Student 2,022 PointsDRY stands for Don't Repeat Yourself.