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 trialMichael Smith
11,803 PointsHow's this attempt before I watch the solution?
/*
Prompts for two numbers, stores them as variables and passes them to the function which returns a random number between the two
*/
//Defines the function
function getRandomFromRange(valueOne, valueTwo) {
var result = Math.floor(Math.random() * (valueOne - valueTwo + 1)) + valueTwo;
alert("Your random number is: " + result);
}
//Prompts for the numbers
var stringFirstNumber = prompt("Give me a large number:");
var intFirstNumber = parseInt(stringFirstNumber);
var stringSecondNumber = prompt("Give me small number:");
var intSecondNumber = parseInt(stringSecondNumber);
//Calls the function
getRandomFromRange(intFirstNumber, intSecondNumber);
1 Answer
Michael Smith
11,803 PointsOk, I watched the video and modified to this:
/*
Prompts for two numbers, stores them as variables and passes them to the function
which returns a random number between the two
*/
//Defines the function
function getRandomFromRange(valueOne, valueTwo) {
return Math.floor(Math.random() * (valueOne - valueTwo + 1)) + valueTwo;
}
//Prompts for the numbers
var stringFirstNumber = prompt("Give me a large number:");
var intFirstNumber = parseInt(stringFirstNumber);
var stringSecondNumber = prompt("Give me small number:");
var intSecondNumber = parseInt(stringSecondNumber);
//Calls the function
alert(getRandomFromRange(intFirstNumber, intSecondNumber));
Iain Simmons
Treehouse Moderator 32,305 PointsIain Simmons
Treehouse Moderator 32,305 PointsGood idea. It's always best to have a function perform only one... function!
alert
already performs the task of displaying a message to a user. If you wanted to use the random number for something else, you would have needed to remove thealert
from your original function.