Question about standard answer:

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

Link to the challenge:

But you don’t do anything if your randomNumber is 0 or 1… This isn’t quite correct

So your solution works, but it does a lot of unnecessary work.

If we look at the documentation for the Math.random() method, we can see:

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.

1 Like

The solution doesn’t quite work. The instructions want a 0 to never be returned

The instructions show

Change randomFraction to return a random number instead of returning 0.

Which to me reads as return a random number (which could be 0) instead of returning only 0.

(If you run the code it passes)

Intended solution:

It usually passes, unless the RNG returns 0

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.

1 Like

Yeah, I remember some discussion about clarifying this challenge but I couldn’t locate the issue.

Then change the name as well. This challenge is just odd if you ask me.

I think the original idea was something a tiny bit more than a straight copy-paste of the example.

1 Like

It seems an odd way of introducing Math.random.

Also, the fail state of the challenge is random. Which is also a bit strange. You can pass the challenge with “incorrect” code, by chance.

I’m open to replacing it with literally anything that isn’t copy-pasting the example syntax.

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.

Well, the result has to be in the range 0-1 (excluding 1), but we could make it request a random decimal between 0.5 and 0.6?

I think that’s a nice compromise and sets up the next lesson pretty well.

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?

2 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.