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
Thank you for your response.
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 ?
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
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.
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.