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
Mohamed Saied
Courses Plus Student 293 PointsJavaScript Random numbers generator
in the "JavaScript Full-Stack developer" in "Random Numbers" lessons. we use Math.Floor to generate Integers , but why we don't use Math.round ? like this, Math.round(Math.random() * 6)
and a second question :
can we use this code : var input = parseInt( prompt("your age") )
instead of this : var input = prompt("your age"); var topNumber = parseInt(input);
???
1 Answer
Samuel Ferree
31,723 PointsMath.round would generate a set of numbers that doesn't falls within your expected range, and it wouldn't be statistically accurate. Goal Table:
| Value | Probability |
|---|---|
| 0 | 1/6 |
| 1 | 1/6 |
| 2 | 1/6 |
| 3 | 1/6 |
| 4 | 1/6 |
| 5 | 1/6 |
vs what you would get with math.round
| Value | Probability |
|---|---|
| 0 | 1/12 |
| 1 | 1/6 |
| 2 | 1/6 |
| 3 | 1/6 |
| 4 | 1/6 |
| 5 | 1/6 |
| 6 | 1/12 |
Per your second question, you could use that line of code, but you would lose the user input, would could be useful if they made a mistake, like so:
var input = prompt("your age");
var topNumber = parseInt(input);
if(topNumber === NaN) {
alert("Sorry " + input + " is not a number");
}