Tell us what’s happening:
The instructions for this lesson ask you to use the Math.floor(Math.random()) * N to generate and return a random whole number between 0 and 9
When I return Math.floor(Math.random()*9) it is not accepted.
The error says " You should have multiplied the result of Math.random by 10 to make it a number that is between zero and nine."
Math.random() will return a value between 0 and 1, so it is possible for it to return .9. If I multiply by 10 It is possible that my result will be 9, which is not between zero and nine, it IS 9, which should fail the test.
Your code so far
function randomWholeNum() {
// Only change code below this line
return Math.floor(Math.random()*10);
}
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36
Challenge: Basic JavaScript - Generate Random Whole Numbers with JavaScript
So, if you were asked to write a function to calculate the amount of space BETWEEN two parked vehicles you would include the space that those vehicles occupy?
Use another function, Math.floor() to round the number down to its nearest whole number.
Remember that Math.random() can never quite return a 1 and, because we’re rounding down, it’s impossible to actually get 20. This technique will give us a whole number between 0 and 19.
This states that “a whole number between 0 and 19” includes both 0 and 19.
I would guess that 99.999999% of the people taking this course are not computer scientists.
I am not a computer scientist, so to me between means the area separating 2 things, which by default means exclusive. If the term between has a different meaning in javascript that should be clearly explained before it is used.
I am not whining or complaining. I would like to see this course get better. I have struggled at times because of the language being used in the lessons and challenges.
I can’t change the conventional use of that word in computer science for you, unfortunately.
Like I said, maybe your text doesn’t have the full explanation that shows what they mean? This isn’t some special Javascript only usage of the term.
The usage of “between” in MDN can be even a bit more complicated unfortunately, as they are inclusive of the lower and exclusive of the upper in the first two links here:
The last one uses language similar to the next challenge:
The word “between” even when clarifying it using inclusive/exclusive is easy to confuse. I feel like “range” or using from/to or starting/ending would be easier to understand.
This technique will give us a whole number ranging from 0 to 19
This technique will give us a whole number starting from 0 and ending with 19
This function returns a Number value with positive sign, greater than or equal to +0𝔽 but strictly less than 1𝔽, chosen randomly or pseudo randomly with approximately uniform distribution over that range, using an implementation-defined algorithm or strategy.
We can use the whole phrase “a whole number bigger than or equal to 0 and less than or equal to 19.” if we are concerned that “between” isn’t being understood by learners generally.
Since whenever it was last updated, this Challenge has received nearly zero questions while the next Challenge has received many more questions, so I don’t know that this change will change much?
We can generate random decimal numbers with Math.random(), but sometimes we need to generate random whole numbers. The following process will give us a random whole number less than 20:
Use Math.random() to generate a random decimal number.
Multiply that random decimal number by 20.
Use Math.floor() to round this number down to the nearest whole number.
Remember that Math.random() can never quite return a 1, so it’s impossible to actually get 20 since we are rounding down with Math.floor(). This process will give us a whole number bigger than or equal to 0 and less than or equal to 19.
Putting everything together, this is what our code looks like:
Math.floor(Math.random() * 20);
We are calling Math.random(), multiplying the result by 20, then passing the value to Math.floor() function to round the value down to the nearest whole number.
Use this technique to generate and return a random whole number in the range from 0 to 9.
We can generate random decimal numbers with Math.random(), but sometimes we need to generate random whole numbers. The following process will give us a random whole number less than 20:
Use Math.random() to generate a random decimal number.
Multiply that random decimal number by 20.
Use Math.floor() to round this number down to the nearest whole number.
Remember that Math.random() can never quite return a 1, so it’s impossible to actually get 20 since we are rounding down with Math.floor(). This process will give us a whole number in the range from 0 to 19.
Putting everything together, this is what our code looks like:
Math.floor(Math.random() * 20);
We are calling Math.random(), multiplying the result by 20, then passing the value to Math.floor() function to round the value down to the nearest whole number.
Use this technique to generate and return a random whole number in the range from 0 to 9.
While the getRandomInt() function above is inclusive at the minimum, it’s exclusive at the maximum. What if you need the results to be inclusive at both the minimum and the maximum? The getRandomIntInclusive() function below accomplishes that.
function getRandomIntInclusive(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1) + min); // The maximum is inclusive and the minimum is inclusive
}
If you’re opening an issue, maybe include the above in the lesson?