Reputation: 352
My JavaScript program is supposed to print out random numbers, between two max and min values given by the user. It works great with pre-input numbers, but as soon as I introduce the prompts to get max and min, the numbers start printing out funny.
var min, max, hmany, i;
var n, w, ans, rsp;
min = prompt("Minimum number for addition problems");
//console.log(min);
max = prompt("Maximum number for addition problems");
//console.log(max);
hmany = prompt("How many questions do you want to answer?");
for (i = 0; i < 10; i++)
{
n = Math.floor(Math.random() * (max - min + 1)) + min;
console.log(n);
}
Sample Output:(when given 1 for min and 10 for max)
21
61
71
51
41
01
01
11
21
11
Upvotes: 0
Views: 52
Reputation: 382102
Right now, you're doing operations on strings. And when the min is "1", what you do is
n = someOperation + "1",
which gives a string ending in "1"
.
You have to parse the strings to numbers :
min = parseFloat(prompt("Minimum number for addition problems"));
Upvotes: 3