mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-05-28 18:26:54 +00:00
feat(curriculum): add factorial lab (#55889)
Co-authored-by: Dario-DC <105294544+Dario-DC@users.noreply.github.com>
This commit is contained in:
@@ -1955,7 +1955,10 @@
|
||||
"dvnt": { "title": "164", "intro": [] },
|
||||
"ekdb": { "title": "165", "intro": [] },
|
||||
"ezek": { "title": "166", "intro": [] },
|
||||
"vabq": { "title": "167", "intro": [] },
|
||||
"lab-factorial-calculator": {
|
||||
"title": "Build a Factorial Calculator ",
|
||||
"intro": ["In this lab, you will build a factorial calculator."]
|
||||
},
|
||||
"lgfm": { "title": "168", "intro": [] },
|
||||
"kgtw": { "title": "169", "intro": [] },
|
||||
"ljmq": { "title": "170", "intro": [] },
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
---
|
||||
title: Introduction to the Build a Factorial Calculator
|
||||
block: lab-factorial-calculator
|
||||
superBlock: front-end-development
|
||||
---
|
||||
|
||||
## Introduction to the Build a Factorial Calculator
|
||||
|
||||
In this lab, you will build a factorial calculator.
|
||||
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"name": "Build a Factorial Calculator ",
|
||||
"isUpcomingChange": true,
|
||||
"usesMultifileEditor": true,
|
||||
"blockType": "lab",
|
||||
"dashedName": "lab-factorial-calculator",
|
||||
"order": 167,
|
||||
"superBlock": "front-end-development",
|
||||
"challengeOrder": [{ "id": "66c07238b01053abaf812065", "title": "Build a Factorial Calculator" }],
|
||||
"helpCategory": "JavaScript"
|
||||
}
|
||||
+133
@@ -0,0 +1,133 @@
|
||||
---
|
||||
id: 66c07238b01053abaf812065
|
||||
title: Build a Factorial Calculator
|
||||
challengeType: 14
|
||||
dashedName: build-a-factorial-calculator
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
A factorial is the product of an integer and all the integers below it. For example, the factorial of `5` is `5 * 4 * 3 * 2 * 1 = 120`. In this lab, you will create a factorial calculator that takes a number from the user and calculates the factorial of that number.
|
||||
|
||||
**Objective:** Fulfill the user stories below and get all the tests to pass to complete the lab.
|
||||
|
||||
**User Stories:**
|
||||
|
||||
1. You should declare a variable `num` and assign it a number of your choice. The assigned number should be between `1` and `20` inclusive.
|
||||
1. Create a function named `factorialCalculator` that takes a number as an argument and returns the factorial of that number.
|
||||
1. Inside the function, declare a `result` variable and assign it the value of `1`. Using a loop, loop through all numbers from `1` to the input number(inclusive) and for each number, multiply the `result` variable by the current number and assign the result to the `result` variable. You can choose to use either a `for` loop, `while` loop or `do...while` loop here.
|
||||
1. You should call the `factorialCalculator` function with with `num` as the argument and assign the result to the variable `factorial`.
|
||||
1. You should store the final output in the format `Factorial of [num] is [factorial]` and assign it to the variable `resultMsg`.
|
||||
1. You should output the value of `resultMsg` to the console.
|
||||
|
||||
# --hints--
|
||||
|
||||
You should have a `num` variable.
|
||||
|
||||
```js
|
||||
assert.isDefined(num);
|
||||
```
|
||||
|
||||
You should assign a value to the `num` variable
|
||||
|
||||
```js
|
||||
assert.isNotNull(num);
|
||||
```
|
||||
|
||||
The value of `num` should be between `1` and `20`.
|
||||
|
||||
```js
|
||||
assert.isAtLeast(num, 1);
|
||||
assert.isAtMost(num, 20);
|
||||
```
|
||||
|
||||
The `num` value should be a number.
|
||||
|
||||
```js
|
||||
assert.isNumber(num);
|
||||
```
|
||||
|
||||
You should have a function named `factorialCalculator`.
|
||||
|
||||
```js
|
||||
assert.isFunction(factorialCalculator);
|
||||
```
|
||||
|
||||
The `factorialCalculator` function should take a number as an argument.
|
||||
|
||||
```js
|
||||
assert.match(factorialCalculator.toString(), /\s*factorialCalculator\(\s*\w+\s*\)/);
|
||||
```
|
||||
|
||||
The factorial of `5` should return `120`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(factorialCalculator(5), 120);
|
||||
```
|
||||
|
||||
The factorial of `7` should return `5040`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(factorialCalculator(7), 5040);
|
||||
```
|
||||
|
||||
For `5`, the `resultMsg` should contain `Factorial of 5 is 120`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(resultMsg, 'Factorial of 5 is 120');
|
||||
```
|
||||
|
||||
You should call your `factorialCalculator` function with the `num` variable as the argument.
|
||||
|
||||
```js
|
||||
assert.match(__helpers.removeJSComments(code), /factorialCalculator\(\s*num\s*\)\s*;?\s?$/m);
|
||||
```
|
||||
|
||||
You should define a `factorial` variable and assign the result of the `factorialCalculator` function to it.
|
||||
|
||||
```js
|
||||
assert.isDefined(factorial);
|
||||
assert.strictEqual(factorial, factorialCalculator(num));
|
||||
```
|
||||
|
||||
Your `factorialCalculator` should produce the correct result.
|
||||
|
||||
```js
|
||||
assert.strictEqual(factorialCalculator(6), 720);
|
||||
assert.strictEqual(factorialCalculator(9), 362880);
|
||||
assert.strictEqual(factorialCalculator(11), 39916800);
|
||||
```
|
||||
|
||||
You should output the value of `resultMsg` to the console.
|
||||
|
||||
```js
|
||||
assert.match(__helpers.removeJSComments(code), /console\.log\(resultMsg\)\s*;?\s?$/m);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const num = 5;
|
||||
|
||||
function factorialCalculator(n) {
|
||||
let result = 1;
|
||||
|
||||
for (let i = 2; i <= n; i++) {
|
||||
result *= i;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
const factorial = factorialCalculator(num);
|
||||
const resultMsg = `Factorial of ${num} is ${factorial}`;
|
||||
console.log(resultMsg);
|
||||
```
|
||||
Reference in New Issue
Block a user