Challenge not accepting console.log way

Tell us what’s happening:
Hello, I am having trouble with understanding that why is FCC not accepting when I put console.log around Math.random, but it accepts the other two ways, can someone explain?

Your code so far


function randomFraction() {

// Only change code below this line
return console.log(Math.random());

// Only change code above this line
}

However below two methods work : 1.

function randomFraction() {

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

  // Only change code above this line
}
2.
function randomFraction() {

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

  // 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/86.0.4240.111 Safari/537.36.

Challenge: Generate Random Fractions with JavaScript

Link to the challenge:

dont know but its bad practice either way

Yeah, I get that, but still I want to know what is causing problem here, maybe it has something to do with the Math.random

ive seen a lot of people have issues with tests not passing when console is used. i never got an answer to why either

console.log() returns undefined.
According to the instructions: randomFraction should return a random number.

If you want to see the number being returned by using console.log, then you’ll need to

  1. save the value returned from Math.random() to a variable
  2. call console.log(), passing the variable from 1.
  3. return the variable from 1.
2 Likes

Using console.log only prints your answer to the screen. It does not actually return your value out of your function for another piece of code to use.

You need to return values when the challenge asks you to return something. If you want to view the output, you should wrap the output of your function in a console log.

1 Like