freeCodeCamp Challenge Guide: Generate Random Whole Numbers with JavaScript

freeCodeCamp Challenge Guide: Generate Random Whole Numbers with JavaScript
0.0 0

#1

It’s great that we can create random decimal numbers, but it’s even more useful if we lot more useful to generate a random whole number.

To achieve this we can multiply the random number by ten and use the Math.floor() to convert the decimal number to a whole number

Math.floor(Math.random()*10)

#2

The trick for solving this one was to only change the return line :slight_smile:


#3

You could do it like so:

var randomNumberBetween0and19 = Math.floor(Math.random() * 20);

function randomWholeNum() {

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

return Math.floor(Math.random() * 10);
}


#4

This is a good example, but returning using

return randomNumberBetween0and9;

instead would be a better way.