Generate Whole Number with Javascript

Generate Whole Number with Javascript
0.0 0


I’m still not sure what did I went wrong with this exercise bc I’m still not able to generate a whole number so far. Here’s my screenshot of this code


In the function you are returning Math.random() which returns a random number between 0 and 1.


The problem is as @Robert-96 prophesized :smiley:

function randomNumber() {
    return Math.floor(Math.random()*10) + 1
  • Math.random() gives floating numbers between 0 and 1.
  • Multiply that with 10 and we’ll get a floating number between 0 and 10.
  • Taking Math.floor() of that will give us an integer between 0 and 10.
  • Adding 1 to that will give us a number between 1 and 10.


Ok it didn’t return a whole number. I’m not sure why and I did checked w3school about Javascrpt’s random method for example. It’s the same explanation as you just gave. it’s really strange.

function randomWholeNum(x) {
    return Math.floor((Math.random() * x) +1);

This function will return a whole number between 1 and x.


You are generating the number, but you’re not returning it. You are returning Math.random(), not the number generated on the line above it. The number generated on the line above it does nothing. It’s not assigned to anything, so it is generated but then never used anywhere. You need to actually return the line above it, or assign that line to a variable and return the variable.


I assume that you’ve solved this by now, but just in case - here it is visualized:

function randomWholeNum() {

    num = Math.floor((Math.random()*10) + 1);
    return num;

This will store your randomly selected number inside the variable called num. Returning num should do the trick.