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 trialGrzegorz Zielinski
5,838 PointsFunction problem
Hello,
After I finished one o the videos about functions I tried to write some function by myself to practice my skills a bit - create a function that ask for a low and high number, and then give a random number in range between those two numbers (Match.random).
Here is the code:
const min = prompt('low'); const max = prompt('high');
function getRandomNumber(min, max) { return Math.floor( Math.random() * (max - min) + min); }
console.log(Random number from ${min} to ${max} is ${getRandomNumber(min, max)}.
);
As a result I'm getting number which is equal to Math.random() * (max - min) instead od Math.floor( Math.random() * (max - min) + min); and it seems like second "min" at the end of calculation is - for some reason, getting ignored by function.
I got pretty confused with this one, cause even MDN says that it funcion should look like that:
function getRandomArbitrary(min, max) { return Math.random() * (max - min) + min; }
What looks exactly the same as my way of doing it.
Please help me with this one, thanks!
1 Answer
ILYAS KERBAL
Front End Web Development Techdegree Graduate 15,209 PointsHello the code to generate a random number is the following
let mini = prompt('Insert min: ');
let maxi = prompt('Insert max: ');
let randomNumber = Math.floor(Math.random() * ((maxi-mini)+1)) + mini;
console.log(`Random number from ${mini} to ${maxi} is ${randomNumber}.`);
Grzegorz Zielinski
5,838 PointsGrzegorz Zielinski
5,838 PointsThank you for your answer, but that's not what I asked about.
What I'm looking for is the way of getting a random number from range of certain min and max number, but as a FUNCTION. Following that, I'm also looking for that answer = why is the function ignoring second 'min' in my code.
Thanks