mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-05-28 18:26:54 +00:00
feat(curriculum): add nth-fibonacci number generator lab (#56026)
Co-authored-by: Dario-DC <105294544+Dario-DC@users.noreply.github.com>
This commit is contained in:
@@ -2103,7 +2103,12 @@
|
||||
"ybrh": { "title": "271", "intro": [] },
|
||||
"vjgg": { "title": "272", "intro": [] },
|
||||
"kaui": { "title": "273", "intro": [] },
|
||||
"afhj": { "title": "274", "intro": [] },
|
||||
"lab-nth-fibonacci-number-generator": {
|
||||
"title": "Build the nth Fibonacci number generator",
|
||||
"intro": [
|
||||
"For this lab, you will implement the nth Fibonacci number generator."
|
||||
]
|
||||
},
|
||||
"cfyv": { "title": "275", "intro": [] },
|
||||
"sgau": { "title": "276", "intro": [] },
|
||||
"clak": { "title": "277", "intro": [] },
|
||||
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
---
|
||||
title: Introduction to the Build the nth Fibonacci number generator
|
||||
block: lab-nth-fibonacci-number-generator
|
||||
superBlock: front-end-development
|
||||
---
|
||||
|
||||
## Introduction to the Build the nth Fibonacci number generator
|
||||
|
||||
For this lab, you will implement the nth Fibonacci number generator.
|
||||
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"name": "Build the nth Fibonacci number generator",
|
||||
"isUpcomingChange": true,
|
||||
"usesMultifileEditor": true,
|
||||
"dashedName": "lab-nth-fibonacci-number-generator",
|
||||
"order": 274,
|
||||
"superBlock": "front-end-development",
|
||||
"challengeOrder": [{ "id": "66d9af3897e7d75a895b72c2", "title": "Build the nth Fibonacci number generator" }],
|
||||
"helpCategory": "JavaScript",
|
||||
"blockType": "lab"
|
||||
}
|
||||
+131
@@ -0,0 +1,131 @@
|
||||
---
|
||||
id: 66d9af3897e7d75a895b72c2
|
||||
title: Build the nth Fibonacci number generator
|
||||
challengeType: 14
|
||||
dashedName: lab-nth-fibonacci-number-generator
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
In this lab you will use a dynamic programming approach to compute and return the `n`-th number from the Fibonacci sequence, in which each number is the sum of the two preceding numbers.
|
||||
|
||||
**Objective:** Fulfill the user stories below and get all the tests to pass to complete the lab.
|
||||
|
||||
**User Stories:**
|
||||
|
||||
1. You should define an array named `sequence` in the global scope. It should contain the number `0` and `1`.
|
||||
1. You should create a function named `fibonacci`.
|
||||
1. The `fibonacci` function should take a single parameter, which is a positive integer.
|
||||
1. If the input is not a positive integer, the function should return `"Invalid input"`.
|
||||
1. A call to `fibonacci(n)` should use a dynamic programming approach to compute and return the `n`-th number from the Fibonacci sequence.
|
||||
1. Each computed number at the position `n` in the Fibonacci sequence should be stored at the index `n - 1` of the `sequence` array.
|
||||
1. If the input number requests an already computed number, no new computations should happen and the number should be retrieved from the `sequence` array.
|
||||
|
||||
# --hints--
|
||||
|
||||
You should have a `sequence` array.
|
||||
|
||||
```js
|
||||
assert.isArray(sequence);
|
||||
```
|
||||
|
||||
The `sequence` array should contain `0` and `1` at definition.
|
||||
|
||||
```js
|
||||
assert.match(__helpers.removeJSComments(code), /(let|const)\s+sequence\s*=\s*\[\s*0\s*,\s*1\s*\]\s*;?/)
|
||||
```
|
||||
|
||||
You should create a `fibonacci` function.
|
||||
|
||||
```js
|
||||
assert.isFunction(fibonacci);
|
||||
```
|
||||
|
||||
`fibonacci(3.5)` should return `"Invalid input"`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(fibonacci(3.5), "Invalid input")
|
||||
```
|
||||
|
||||
`fibonacci(0)` should return `"Invalid input"`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(fibonacci(0), "Invalid input")
|
||||
```
|
||||
|
||||
`fibonacci(-1)` should return `"Invalid input"`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(fibonacci(-1), "Invalid input")
|
||||
```
|
||||
|
||||
`fibonacci("three")` should return `"Invalid input"`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(fibonacci("three"), "Invalid input")
|
||||
```
|
||||
|
||||
`fibonacci(1)` should return `0`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(fibonacci(1), 0)
|
||||
|
||||
```
|
||||
|
||||
`fibonacci(32)` should return `1346269`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(fibonacci(32), 1346269)
|
||||
|
||||
```
|
||||
|
||||
`fibonacci(5)` should return `3`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(fibonacci(5), 3);
|
||||
|
||||
```
|
||||
|
||||
`sequence` should contain numbers up to the highest call so far.
|
||||
|
||||
```js
|
||||
sequence.length = 2
|
||||
fibonacci(2)
|
||||
assert.lengthOf(sequence, 2)
|
||||
fibonacci(5)
|
||||
assert.lengthOf(sequence, 5)
|
||||
fibonacci(3)
|
||||
assert.lengthOf(sequence, 5)
|
||||
fibonacci(10)
|
||||
assert.lengthOf(sequence, 10)
|
||||
assert.deepEqual(sequence, [0, 1, 1, 2, 3, 5, 8, 13, 21, 34])
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const sequence = [0, 1];
|
||||
|
||||
const fibonacci = (n) => {
|
||||
if (typeof n !== "number" || n < 1 || n % 1 !== 0) {
|
||||
console.log("Invalid input")
|
||||
return "Invalid input"
|
||||
}
|
||||
if (n <= sequence.length) {
|
||||
return sequence[n - 1];
|
||||
}
|
||||
for (let i = sequence.length; i < n; i++) {
|
||||
sequence.push(sequence[i - 1] + sequence[i - 2]);
|
||||
}
|
||||
return sequence[n - 1]
|
||||
}
|
||||
|
||||
```
|
||||
Reference in New Issue
Block a user