Back to Freecodecamp

Step 9

curriculum/challenges/english/blocks/workshop-mathbot/69218e46f4490b33f2481a1a.md

latest1.9 KB
Original Source

--description--

The formula to generate a random integer between two values is the following:

js
Math.floor(Math.random() * (maximum - minimum) + minimum);

This will produce a result that is a integer between two values.

Create a variable called randomInt and assign it the result of generating a value between the min and max values.

Then, log the randomInt variable to see the result. Try refreshing the page to see different results.

--hints--

You should have a variable called randomInt.

js
assert.isNotNull(randomInt);

Your randomInt variable should be a number.

js
assert.isNumber(randomInt);

You should assign the result of generating a value between the min and max values. Make sure not to hardcode a value. You should use the formula provided above.

js
assert.isAtLeast(randomInt, min);
assert.isAtMost(randomInt, max);

// check for whole random numbers just in case a camper does this
assert.notMatch(code, /randomInt\s*=\s*\d+;?/);

You should log the randomInt variable to the console.

js
assert.match(code, /console\.log\(\s*randomInt\s*\);?/);

--seed--

--seed-contents--

js
const botName = "MathBot";
const greeting = `Hi there! My name is ${botName} and I am here to teach you about the Math object!`;

console.log(greeting);

console.log("The Math.random() method returns a pseudo random number greater than or equal to 0 and less than 1.");

const randomNum = Math.random();
console.log(randomNum);

console.log("Now, generate a random number between two values.");

const min = 1;
const max = 100;

const randomNum2 = Math.random() * (max - min) + min;
console.log(randomNum2);

console.log("The Math.floor() method rounds the value down to the nearest whole integer.");

const numRoundedDown = Math.floor(6.7);
console.log(numRoundedDown);

console.log("Now, generate a random integer between two values.");
--fcc-editable-region--

--fcc-editable-region--