# Math.random Woes

Tell us what’s happening:
Describe your issue in detail here.

I took a peek at the solution already and i get it, I see how that is a simple and elegant solution to the problem. But, why does my code not work just as well. The logic seems to check out

function randomFraction() {
let result = 0;
Math.random(result);
if (result = 0){
Math.random(result);
}
else{
return(result);
}
}

``````  **Your code so far**
``````
``````function randomFraction() {

// Only change code below this line
let result = 0;
Math.random(result);
if (result = 0){
Math.random(result);
}
else{
}
return(result);

// Only change code above this line
}
``````
``````  **Your browser information:**
``````

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36`

Challenge: Generate Random Fractions with JavaScript

HI @eziolise !

I see a few issues.

You are using the assignment operator here.
But that is not the same as equality operator.

You are creating a random number but you don’t seen to be doing anything with it here.

You don’t seem to be using this else clause, so you can just get rid of it.

Hope that helps!

2 Likes

i fixed issues number 1 and 3, and i understand what you are saying in with Issue number 2. But, doesn’t the random number generated get used in the “if statement” right below ?

Here is your current code cleaned up.

``````function randomFraction() {

// Only change code below this line
let result = 0;

if (result === 0) {
Math.random();
}
console.log(result)
return (result);

// Only change code above this line
}
``````

I added a console.log to show you what the result variable is currently returning.

You need to modify the inside of that if statement so result returns a random number.

Also, remember that you don’t need to place anything inside the `Math.random()` parenthesis

1 Like

When i run your above code i get a result == 0, which fails the second requirement for this challenge.

but I think i get your point now.

I for some reason expected it to run the “if statement as a loop” until
" Math.random != 0" .

But i would need a “for loop statement” or a “while statement” for that to play out right ?

That’s one way to do it and the preferred way.

You could pass with your code but adjusting the line inside the if statement.

I will give you another hint.

Right now this is not assigned to anything

Remember that the directions say to return a random number instead of returning `0` .

So if you make that small change inside your if statement, then the test will pass.

Hope that helps!

1 Like

The reason why the guide uses a while loop is because we are trying to ensure that we don’t get back 0.

While you could pass with your current code with the if statement, you still run into the possibility that Math.random() would return 0.

That is the main difference between your approach and the guide’s solution.

But regardless of the two approaches to the problem, you still have to assign your Math.random() to something so it can return something other than 0.
That is why you are not passing yet.

Hope that is clearer

1 Like

Thank you so much, really appreciate the assistance.

I got this to work two ways - first i got it to work like this.

function randomFraction() {

// Only change code below this line

for (let x = Math.random(); x != 0;){
return x;
}

// Only change code above this line
}

But I really wanted to understand this as deeply as possible so literally stared at the screen till i figure your hint out and assigned the “if statement” properly.

Such a small piece of code and not the most complicated i’ve encountered but solving this gave me a surge of motivation.

Thanks again for your assistance !

This is not how you write a for loop. Don’t do this. Ignoring the third piece of the loop head is a good sign that you’re misusing the for loop.

Also, you alway return on the first iteration, no matter what, so this doesn’t do what you are trying to do.

thanks for pointing this out. That’s what i get for taking shortcuts

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