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 trialerdragerdragsson
Courses Plus Student 5,887 PointsAm i getting this right?
var randomNumber = prompt("type a number");
var randomNumber2 = prompt("type a new number");
var input2 = parseInt(randomNumber2);
var input = parseInt(randomNumber);
var Random = Math.floor(Math.random() * (input2 - input + 1)) + input;
document.write(Random);
okey so the first variable "randomNumber" holds the input from the user, and so does the second "randomNumber2"
and the "input2" and "input" variable, uses the parseInt statement to make the string into integers,
and now to the var Random part where you get a random number, i dont understand that one fully, can someone explain that line of code so i can understand it, i would appreciate it.
Kind regards
// erdrag
1 Answer
Jonathan Grieve
Treehouse Moderator 91,253 PointsHi ther Erfdrgag,
Assuming that code is correct it looks like you're using 2 methods, floor and random to create a random number. When you use random()
to make a random number you're actually getting a random number between 0 and 1, so it outputs something like 0.33432. That's where floor()
comes in. It floors it to the lowest whole value which in this case is zero.
That said if I was to tackle this, I'd try something like
var Random = Math.floor(Math.random(input2 - input) * 1;
This should calculate the random number between the result of input 2 and input 1 making it a whole number. This way, floor doesn't round dow to 0 so you have something to calculate.
Hope this works, :)
erdragerdragsson
Courses Plus Student 5,887 Pointserdragerdragsson
Courses Plus Student 5,887 Pointshey, thanks for replying so quick,
in this line of code
Math.random(input2 - input)
what does the "input2 - input" mean? am i subtracting input2 with input? and if so, why?
sorry for asking so much, :S
Jonathan Grieve
Treehouse Moderator 91,253 PointsJonathan Grieve
Treehouse Moderator 91,253 PointsHi there :)
As I understand it you're looking for a random number between the number values assigned by the prompt function, which could be any 2 numbers.
But I've looked at it again for you and it's a little more complicated than I remember. Rather than just choosing any 2 numbers on the fly the challenge is looking for something a little more specific like a number for an upper limit and one for a lower limit
Any have a look at this forum post with a solution to the challenge which will explain it for you better than I have. :)
https://teamtreehouse.com/forum/simple-solution
erdragerdragsson
Courses Plus Student 5,887 Pointserdragerdragsson
Courses Plus Student 5,887 PointsThanks for replying, i read the forum post, but it does not say why you subtract input2 with input.
And The line of code you wrote first, why do you multiply the input with 1?
// erdrag
Jonathan Grieve
Treehouse Moderator 91,253 PointsJonathan Grieve
Treehouse Moderator 91,253 PointsI believe that's explained here in Marcus Parson's point.
"In order to get to that last possible upperLimit, all you have to do is add 1 to your (upperLimit - lowerLimit) statement like so:"
It's increasing the range of the random number available, so rather than just returning a random number between 0 and 1, which is all the random function does you'll get a random number of maximum upper limit number.
So if you had an upper limit of 10 but a lower of 4 this would enable you get a possible return of 10.
erdragerdragsson
Courses Plus Student 5,887 Pointserdragerdragsson
Courses Plus Student 5,887 PointsI understanderino the +1 part of it, but why do u use the "-" sign between the variables, and in Marcus line of code ar the end he uses +lowerlimit but you used *1, what is the difference?
Jonathan Grieve
Treehouse Moderator 91,253 PointsJonathan Grieve
Treehouse Moderator 91,253 PointsBecause you're looking for a number in the range of those 2 variables. I don't understand it fully myself but way I see it is when you do the subtraction (which is what the - operator does) you use that difference and multiply it by the lowerlimit number.
That's what Marcus did and I didn't. :-)