Learn Functional Programming by Building a Spreadsheet - Step 99

Hi FCC,

Guess I spotted a little mistake.
The tests for the random function on step 99 are not accurate.

And on step 100 the random function that is written is incorrect:
random: ([x, y]) => Math.floor(Math.random() * y + x)
(which passes the tests on step 99)

First consideration:
x = 2, y = 10 , Math.random= 0.999
Math.floor(0.999 * 10 + 2) = 11

Second consideration:
if x is bigger than y the function doesn’t work correctly.

To provide a random integer between two numbers - x (included) and y (included):
random: ([x, y]) => Math.floor(Math.random() * (x - y + 1) + y)

Correct me if I am wrong. Math is not my strong point:\

1 Like

I think you are correct here. I’m no mathematician either, but a quick google search shows that the version you are suggesting is the correct solution for finding a random number between any two numbers (inclusive).

I think the version that is currently in there only works if the minimum value passed in is 1. Wait, that might be wrong as well. I think you would still have to take the floor of just Math.random() * y and then add x. So I’m not sure that what is in there now works for anything? But I know there are some very clever math people who hang out here, so we’ll see what they have to say.

P.S. In order to attract more attention to this post, you might want to change the title to reflect the true nature of the issue. Perhaps something like “Incorrect random function for Building a Spreadsheet - Step 99”.

Correct. The provided code in step 100
random: ([x, y]) => Math.floor(Math.random() * y + x)
works if x=1 (but just for this case only).
So the tests on step 99 are not accurate to let this function pass.

Cant’t find a button to edit the title of the post nor the post itself. Guess I don’t have the necessary trust level. Feel free to edit the title.

Corrected random function provided in step 100:
random: ([x, y]) => Math.floor(Math.random() * (x - y + 1) + y)
Works for all cases.

I would also recommend to change the description to point out that the output should be a whole number or integer and x,y is inclusive.

Great tutorial. Thx FCC