mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-09-24 23:01:18 +08:00
feat(curriculum): added 2 new steps to mathbot project for random integer generation (#64064)
Co-authored-by: majestic-owl448 <26656284+majestic-owl448@users.noreply.github.com> Co-authored-by: Dario <105294544+Dario-DC@users.noreply.github.com>
This commit is contained in:
co-authored by
majestic-owl448
Dario
parent
a89054d9db
commit
1e28b76bf5
@@ -33,7 +33,7 @@ Your `randomNum2` variable should be a number.
|
||||
assert.isNumber(randomNum2);
|
||||
```
|
||||
|
||||
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 formulae provided above.
|
||||
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(randomNum2, min);
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
---
|
||||
id: 66eb480d996af5918d7c9c86
|
||||
title: Step 8
|
||||
title: Step 10
|
||||
challengeType: 1
|
||||
dashedName: step-8
|
||||
dashedName: step-10
|
||||
---
|
||||
|
||||
# --description--
|
||||
@@ -46,6 +46,11 @@ console.log("The Math.floor() method rounds the value down to the nearest whole
|
||||
|
||||
const numRoundedDown = Math.floor(6.7);
|
||||
console.log(numRoundedDown);
|
||||
|
||||
console.log("Now, generate a random integer between two values.");
|
||||
|
||||
const randomInt = Math.floor(Math.random() * (max - min) + min);
|
||||
console.log(randomInt);
|
||||
--fcc-editable-region--
|
||||
|
||||
--fcc-editable-region--
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
---
|
||||
id: 66eb488c0129e192542f0865
|
||||
title: Step 9
|
||||
title: Step 11
|
||||
challengeType: 1
|
||||
dashedName: step-9
|
||||
dashedName: step-11
|
||||
---
|
||||
|
||||
# --description--
|
||||
@@ -80,6 +80,11 @@ console.log("The Math.floor() method rounds the value down to the nearest whole
|
||||
const numRoundedDown = Math.floor(6.7);
|
||||
console.log(numRoundedDown);
|
||||
|
||||
console.log("Now, generate a random integer between two values.");
|
||||
|
||||
const randomInt = Math.floor(Math.random() * (max - min) + min);
|
||||
console.log(randomInt);
|
||||
|
||||
console.log("The Math.ceil() method rounds the value up to the nearest whole integer.");
|
||||
|
||||
--fcc-editable-region--
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
---
|
||||
id: 66eb499511810e932ca7eecc
|
||||
title: Step 10
|
||||
title: Step 12
|
||||
challengeType: 1
|
||||
dashedName: step-10
|
||||
dashedName: step-12
|
||||
---
|
||||
|
||||
# --description--
|
||||
@@ -47,6 +47,11 @@ console.log("The Math.floor() method rounds the value down to the nearest whole
|
||||
const numRoundedDown = Math.floor(6.7);
|
||||
console.log(numRoundedDown);
|
||||
|
||||
console.log("Now, generate a random integer between two values.");
|
||||
|
||||
const randomInt = Math.floor(Math.random() * (max - min) + min);
|
||||
console.log(randomInt);
|
||||
|
||||
console.log("The Math.ceil() method rounds the value up to the nearest whole integer.");
|
||||
|
||||
const numRoundedUp = Math.ceil(3.2);
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
---
|
||||
id: 66ecdcf7c8d9a6f49094ffde
|
||||
title: Step 11
|
||||
title: Step 13
|
||||
challengeType: 1
|
||||
dashedName: step-11
|
||||
dashedName: step-13
|
||||
---
|
||||
|
||||
# --description--
|
||||
@@ -100,6 +100,11 @@ console.log("The Math.floor() method rounds the value down to the nearest whole
|
||||
const numRoundedDown = Math.floor(6.7);
|
||||
console.log(numRoundedDown);
|
||||
|
||||
console.log("Now, generate a random integer between two values.");
|
||||
|
||||
const randomInt = Math.floor(Math.random() * (max - min) + min);
|
||||
console.log(randomInt);
|
||||
|
||||
console.log("The Math.ceil() method rounds the value up to the nearest whole integer.");
|
||||
|
||||
const numRoundedUp = Math.ceil(3.2);
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
---
|
||||
id: 66ece0b518f5dcf5930612ab
|
||||
title: Step 12
|
||||
title: Step 14
|
||||
challengeType: 1
|
||||
dashedName: step-12
|
||||
dashedName: step-14
|
||||
---
|
||||
|
||||
# --description--
|
||||
@@ -47,6 +47,11 @@ console.log("The Math.floor() method rounds the value down to the nearest whole
|
||||
const numRoundedDown = Math.floor(6.7);
|
||||
console.log(numRoundedDown);
|
||||
|
||||
console.log("Now, generate a random integer between two values.");
|
||||
|
||||
const randomInt = Math.floor(Math.random() * (max - min) + min);
|
||||
console.log(randomInt);
|
||||
|
||||
console.log("The Math.ceil() method rounds the value up to the nearest whole integer.");
|
||||
|
||||
const numRoundedUp = Math.ceil(3.2);
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
---
|
||||
id: 66ece147323c91f654bf8dd9
|
||||
title: Step 13
|
||||
title: Step 15
|
||||
challengeType: 1
|
||||
dashedName: step-13
|
||||
dashedName: step-15
|
||||
---
|
||||
|
||||
# --description--
|
||||
@@ -96,6 +96,11 @@ console.log("The Math.floor() method rounds the value down to the nearest whole
|
||||
const numRoundedDown = Math.floor(6.7);
|
||||
console.log(numRoundedDown);
|
||||
|
||||
console.log("Now, generate a random integer between two values.");
|
||||
|
||||
const randomInt = Math.floor(Math.random() * (max - min) + min);
|
||||
console.log(randomInt);
|
||||
|
||||
console.log("The Math.ceil() method rounds the value up to the nearest whole integer.");
|
||||
|
||||
const numRoundedUp = Math.ceil(3.2);
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
---
|
||||
id: 66ece75c0b840df77c7e20f0
|
||||
title: Step 14
|
||||
title: Step 16
|
||||
challengeType: 1
|
||||
dashedName: step-14
|
||||
dashedName: step-16
|
||||
---
|
||||
|
||||
# --description--
|
||||
@@ -47,6 +47,11 @@ console.log("The Math.floor() method rounds the value down to the nearest whole
|
||||
const numRoundedDown = Math.floor(6.7);
|
||||
console.log(numRoundedDown);
|
||||
|
||||
console.log("Now, generate a random integer between two values.");
|
||||
|
||||
const randomInt = Math.floor(Math.random() * (max - min) + min);
|
||||
console.log(randomInt);
|
||||
|
||||
console.log("The Math.ceil() method rounds the value up to the nearest whole integer.");
|
||||
|
||||
const numRoundedUp = Math.ceil(3.2);
|
||||
@@ -101,6 +106,11 @@ console.log(
|
||||
const numRoundedDown = Math.floor(6.7);
|
||||
console.log(numRoundedDown);
|
||||
|
||||
console.log("Now, generate a random integer between two values.");
|
||||
|
||||
const randomInt = Math.floor(Math.random() * (max - min) + min);
|
||||
console.log(randomInt);
|
||||
|
||||
console.log(
|
||||
"The Math.ceil() method rounds the value up to the nearest whole integer."
|
||||
);
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
---
|
||||
id: 69218a15fe536ee1be38aefb
|
||||
title: Step 8
|
||||
challengeType: 1
|
||||
dashedName: step-8
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
The next portion of the workshop is to review how to generate a random integer between two values.
|
||||
|
||||
Start by adding another `console.log()` that logs the message `"Now, generate a random integer between two values."` to the console.
|
||||
|
||||
In the next step, you will generate a random integer between the values of the `min` and `max` variables you have already created.
|
||||
|
||||
# --hints--
|
||||
|
||||
You should have a `console.log()` that logs the message `"Now, generate a random integer between two values."`.
|
||||
|
||||
```js
|
||||
assert.match(code, /console\.log\((`|'|")\s*Now,\s+generate\s+a\s+random\s+integer\s+between\s+two\s+values\.\s*\1\);?/);
|
||||
```
|
||||
|
||||
# --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);
|
||||
|
||||
--fcc-editable-region--
|
||||
|
||||
--fcc-editable-region--
|
||||
```
|
||||
@@ -0,0 +1,85 @@
|
||||
---
|
||||
id: 69218e46f4490b33f2481a1a
|
||||
title: Step 9
|
||||
challengeType: 1
|
||||
dashedName: step-9
|
||||
---
|
||||
|
||||
# --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--
|
||||
```
|
||||
@@ -7,62 +7,22 @@
|
||||
"hasEditableBoundaries": true,
|
||||
"dashedName": "workshop-mathbot",
|
||||
"challengeOrder": [
|
||||
{
|
||||
"id": "66ea7adae8053065a64f9002",
|
||||
"title": "Step 1"
|
||||
},
|
||||
{
|
||||
"id": "66ea7e7399244c6d4b6052a1",
|
||||
"title": "Step 2"
|
||||
},
|
||||
{
|
||||
"id": "66ea7fc048a6e26e33c598fe",
|
||||
"title": "Step 3"
|
||||
},
|
||||
{
|
||||
"id": "66ea8239b9eeb76f12299030",
|
||||
"title": "Step 4"
|
||||
},
|
||||
{
|
||||
"id": "66ea845f54195c7020cc521b",
|
||||
"title": "Step 5"
|
||||
},
|
||||
{
|
||||
"id": "66ea89c25e1bd871688eba6a",
|
||||
"title": "Step 6"
|
||||
},
|
||||
{
|
||||
"id": "66eb3ec3b07dfe8f898646c0",
|
||||
"title": "Step 7"
|
||||
},
|
||||
{
|
||||
"id": "66eb480d996af5918d7c9c86",
|
||||
"title": "Step 8"
|
||||
},
|
||||
{
|
||||
"id": "66eb488c0129e192542f0865",
|
||||
"title": "Step 9"
|
||||
},
|
||||
{
|
||||
"id": "66eb499511810e932ca7eecc",
|
||||
"title": "Step 10"
|
||||
},
|
||||
{
|
||||
"id": "66ecdcf7c8d9a6f49094ffde",
|
||||
"title": "Step 11"
|
||||
},
|
||||
{
|
||||
"id": "66ece0b518f5dcf5930612ab",
|
||||
"title": "Step 12"
|
||||
},
|
||||
{
|
||||
"id": "66ece147323c91f654bf8dd9",
|
||||
"title": "Step 13"
|
||||
},
|
||||
{
|
||||
"id": "66ece75c0b840df77c7e20f0",
|
||||
"title": "Step 14"
|
||||
}
|
||||
{ "id": "66ea7adae8053065a64f9002", "title": "Step 1" },
|
||||
{ "id": "66ea7e7399244c6d4b6052a1", "title": "Step 2" },
|
||||
{ "id": "66ea7fc048a6e26e33c598fe", "title": "Step 3" },
|
||||
{ "id": "66ea8239b9eeb76f12299030", "title": "Step 4" },
|
||||
{ "id": "66ea845f54195c7020cc521b", "title": "Step 5" },
|
||||
{ "id": "66ea89c25e1bd871688eba6a", "title": "Step 6" },
|
||||
{ "id": "66eb3ec3b07dfe8f898646c0", "title": "Step 7" },
|
||||
{ "id": "69218a15fe536ee1be38aefb", "title": "Step 8" },
|
||||
{ "id": "69218e46f4490b33f2481a1a", "title": "Step 9" },
|
||||
{ "id": "66eb480d996af5918d7c9c86", "title": "Step 10" },
|
||||
{ "id": "66eb488c0129e192542f0865", "title": "Step 11" },
|
||||
{ "id": "66eb499511810e932ca7eecc", "title": "Step 12" },
|
||||
{ "id": "66ecdcf7c8d9a6f49094ffde", "title": "Step 13" },
|
||||
{ "id": "66ece0b518f5dcf5930612ab", "title": "Step 14" },
|
||||
{ "id": "66ece147323c91f654bf8dd9", "title": "Step 15" },
|
||||
{ "id": "66ece75c0b840df77c7e20f0", "title": "Step 16" }
|
||||
],
|
||||
"helpCategory": "JavaScript"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user