Math.floor and Math.random function

What is wrong with the below code?

It is saying that the below code doesn’t satisfy this requirement: You should have multiplied the result of Math.random by 10 to make it a number that is between zero and nine.

Your code so far


function randomWholeNum() {

// Only change code below this line
var x = Math.random();
var y = x*10;
z = Math.floor(y);
return  z;
}

Your browser information:

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

Challenge: Generate Random Whole Numbers with JavaScript

Link to the challenge:

It appears the test requires you to perform the computation in one line. Try
var z = Math.floor(Math.random() * 10);

That i understood. I even got the answer using that method and using the following code:

function randomWholeNum() {

// Only change code below this line

var x = Math.random()*10;

var y = Math.floor(x);

return y;

}

What I want to understand is why the first method doesn’t work.

1 Like

I can’t really tell why the first one is not passing because i think it is correct too. I guess it has to do with how the test is implemented.

Exactly this. The challenge wants you to use the provided technique.

1 Like

The only problem with your code is that you didn’t declare the variable z.

Edited the function while posting. Declared the variable z while testing.

I don’t think it is a problem with the code.

Right. The only problem with the code you posted is that the variable z isn’t declared. With that fixed, it will work. It fails the test because your solution is different enough from the expected solutions that the tests don’t recognize it. You can move on to the next challenge. For most functions, freeCodeCamp is able to test the results, but that doesn’t really work with random numbers.

Here you go. With the variable z declared. But still showing the same error.

I understood that it is not the problem with the code but because my answer is different.

I have already moved on to the next challenge. Just wanted to know if there was something wrong with the code.

Hi, @sandeepchadaram

I know why its getting an error on your challenge.

  1. The way you do it in the screenshot its correct by creating the 3 variables to output the result, the result of generate random whole numbers, and this is correct to do it too.
function randomWholeNum() {
  var x = Math.random();
  var y = x * 10;
  var z = Math.floor(y);
  
  return z;

};

var result = randomWholeNum();
console.log(result);
  1. But i think the part that you are missing, its that on the challenge it states that you must follow the same logistic of code:

(Note) this paragraph its on the challenge. This line of code must be followed in order to work.
Putting everything together, this is what our code looks like:

// This line of code must be exactly in your code.
Math.floor(Math.random() * 20);

So they multiply the
Math.random() * 10;

That is what they want you to do on challenge, because if you do the same code as above it means that the challenge its programmed to identify same code structure, and if does not meet the criteria code then wont work.
So you should have something like this to work.

function randomWholeNum() {

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

};

var result = randomWholeNum();
console.log(result);

Hope this can help clear the challenge a bit more. Happy coding :slight_smile: