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
Aaron Selonke
10,323 PointsSending an argument/parameter to a function in the form of a universal parameter
//Wanted to try having the user enter their own upper number into the program for the random number generator.abort //The logic of the code always trips up and crashes the browser when I try it out..abort
var upper = prompt("Enter a number as the upper limit that the computer will use to make the random number generator"); upper = parseInt(upper);
function OriginalRandomNumber(upper) { return Math.floor( Math.random() * upper ) + 1; }
var num = OriginalRandomNumber(upper); document.write(num + " is the number!!!!");
var num2 =OriginalRandomNumber(upper);
var counter = 0
while (num !== num2) {
num2 = OriginalRandomNumber();
counter += 1; }
document.write("It took " + counter + " tries for the computer to guess the number!");
2 Answers
Brent Cralley
18,541 PointsI'm not 100% sure, because I didn't try the code out, but following the logic, I'm thinking that every time the variable "upper" is called, it wants to call the prompt for the user to enter it again.
Try changing the name to "enterUpper" when the user enters it, and then use "var upper = parseInt(enterUpper);", and the rest of the code might work.
Just the first thing that I thought to try. Let me know if it works or not!
Aaron Selonke
10,323 PointsStill crashing, but I don't think so becuase so far you can't call a prompt with a variable.