Tell us what’s happening:
Describe your issue in detail here.
My answer is correct. I always check the standard answer even my answer is correct,and I don’t really understand the explanation of the answer.
I want to ask a question about standard answer:
1.Why we have to write: var result = 0; ?
2. I tried to change the code to : var result = 0.1; or var result = 1;
And it doesn’t work.
I kinda feel like I am dumb, cuz I couldn’t understand others’ code. Your code so far
function randomFraction() {
// Only change code below this line
const randomNumber = Math.random();
if (randomNumber > 0 && randomNumber < 1){
return randomNumber;
}
// Only change code above this line
}
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36
Challenge: Basic JavaScript - Generate Random Fractions with JavaScript
The Math.random() static method returns a floating-point, pseudo-random number that’s greater than or equal to 0 and less than 1
This means we can expect 0, or maybe 0.123687, or 0.87837243, all the way to 0.99999
What does that mean? Well if we evaluate your If condition on those values, we can say that it will (almost) always be true, since our input will always be >= 0 and < 1. It will only return false when the number is 0, at which point the function won’t return anything. If we simplify, in order to get a random number instead of 0 we can:
function randomFraction() {
const randomFraction = Math.random();
return randomFraction;
}
This is the same code you had but without the redundant (and with a sneaky bug!) if statement.
OK good to know. That should probably be in the tests, just as a note that says your function should not return 0. There is nothing in the problem as posed that specifies a valid range for the return value other than all decimal numbers (including 0).
Especially since you can pass the tests and proceed without ever realizing that it doesn’t want a 0, unless you get extremely unlucky.
The requirements (or lack of) are weird in this challenge, or at least the way it is written is strange.
The seed function by default returns 0 which is the fail-state of the function. The chance of Math.random() actually returning 0 is pretty small but not impossible (not sure what the actual probability is as it seems implementation specific).
As such, the only reason why you can’t just do return Math.random() is because it might produce the same value as the fail state of the initial function. It is however not specified in the challenge text how to avoid Math.random() returning 0 or why it is even relevant to this challenge.
I would suggest having the initial function return undefined or some other value that isn’t 0 and having that be the initial fail-state to make more sense. I see no reason why the function shouldn’t be allowed to return 0.
Honestly I don’t think even a straight copy paste is detrimental as the very next challenge is a more real-world implementation of Math.random() to return a random integer.
What about something as simple as return a random decimal: 5 <= x < 6?
That way there is some thinking that has to go on, and you have to understand what exactly Math.random() will return, but it’s not as hard as the next challenge.
If we keep it to where the function has to return a decimal, which I guess I’m fine with, the requirements should be stated a bit more clearly and the test should not allow code to pass by chance.
Maybe we can introduce using a min value and have the requirements be return Math.random() + min so we check for a return value with a known range?