freeCodeCamp Challenge Guide: Generate Random Fractions with JavaScript

freeCodeCamp Challenge Guide: Generate Random Fractions with JavaScript
0

#1

JavaScript has a Math.random() function that generates a random decimal number.

Random numbers are useful for creating random behavior.

JavaScript has a Math.random() function that generates a random decimal number between 0 (inclusive) and not quite up to 1 (exclusive). Thus Math.random() can return a 0 but never quite return a 1

Note
Like Storing Values with the Equal Operator, all function calls will be resolved before the return executes, so we can return the value of the Math.random() function.

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

**Warning !!! **
Spoiler Alert !!
A solution to follow:

function randomFraction() {
  // Only change code below this line.
  var result = 0;
  // Math.random() can generate 0. We don't want to return a 0, 
  // so keep generating random numbers until we get one that isn't 0
  while (result === 0) { 
    result = Math.random();
  }
  
  return result;  
  // Only change code above this line.
}

When would it be appropriate to add contents next to the curly brackets for the math.random function?
#2
function randomFraction() {

    // Only change code below this line.

        return 4; // chosen by fair dice roll.
                  // guaranteed to be random.

        // Only change code above this line.
}

#3

Is there anything inherently wrong with what I did?

function randomFraction() {
// Only change code below this line.
var x , a , b;
x= 0;
a = 0;
b = 0;
x += Math.random();
a += Math.random();
b = x/a;
return b;

// Only change code above this line.
}


#4

function randomFraction() {

// Only change code below this line.

return Math.random();

// Only change code above this line.
}


#5

This could generate a 0 no?


#6

If Math.random generates a 0: x += Math.random === 0 which gives a return of 0 ??


#7

This challenge is presenting me with the math.Random() function. math.Random() returns a number between 0 (inclusive) and 1 (exclusive). The challenge wants me to create a function using math.Random() that makes the 0 exclusive.

Can somebody give a hint please?


#8

right,
should be:

function randomFraction() {

// Only change code below this line.

return 1 - Math.random();

// Only change code above this line.
}


#9

Done :wink:

function randomFraction() {

// Only change code below this line.

return Math.random();

// Only change code above this line.
}


#10

Hi, can you give me a lay-mans term on how it works, that is how does Math.random() work?


#11

Math.random() function returns decimal values.
function randomFraction() {

// Only change code below this line
var aa = Math.random();
console.log(aa);
return aa;
// you can see output value within your challenge window, it will clarify the usage of function in detail when you check its output.
// Only change code above this line.
}


#12

My code works but I am unsure that it works for the right reasons, nobody else used the same concept to solve. I would love feedback.

function randomFraction() {

// Only change code below this line.
var num = Math.random();
if (Math.random(num) !== 0) {
return num;
}

return num;

// Only change code above this line.
}


#13

Okay, so it pretty much still returns a decimal value in the console. Now is that because aa stores to what Math.random() is doing, which is to handle numeric values(more than 0 but less than 1). Am I correct in that assumption?


#14

yes correct. This challenge is make campers familiar with the functionality of Math.random() function.


#15

Math.random(); function that generates a random decimal number 0(inclusive). So it is not required to check using if condition because 0 is a integer number.
You can try reversing condition for better clarification
// Only change code below this line.
var num = Math.random();
if (Math.random(num) == 0) {
return num;
}
So extra code not required here.


#16

this code is work, but I want to feedback about this code

function randomFraction() {

// Only change code below this line.
var random = Math.random();
return random;

// Only change code above this line.
}