mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-05-28 18:26:54 +00:00
feat(curriculum): add range of numbers lab to js v9 (#65061)
Co-authored-by: zaira <zairahira@gmail.com>
This commit is contained in:
@@ -6434,6 +6434,13 @@
|
||||
"In this workshop, you'll build a decimal-to-binary converter using JavaScript. You'll practice the fundamental concepts of recursion, explore the call stack, and build out a visual representation of the recursion process through an animation."
|
||||
]
|
||||
},
|
||||
"lab-range-of-numbers": {
|
||||
"title": "Build a Range of Numbers Generator",
|
||||
"intro": [
|
||||
"In this lab, you'll use recursion to generate an array of numbers within a specified range.",
|
||||
"You'll practice recursive function calls, base cases, and building arrays through recursion."
|
||||
]
|
||||
},
|
||||
"lab-permutation-generator": {
|
||||
"title": "Build a Permutation Generator",
|
||||
"intro": [
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
---
|
||||
title: Introduction to the Build a Range of Numbers Generator
|
||||
block: lab-range-of-numbers
|
||||
superBlock: javascript-v9
|
||||
---
|
||||
|
||||
## Introduction to the Build a Range of Numbers Generator
|
||||
|
||||
In this lab, you'll use recursion to generate an array of numbers within a specified range. You'll practice recursive function calls, base cases, and building arrays through recursion.
|
||||
+127
@@ -0,0 +1,127 @@
|
||||
---
|
||||
id: 5cc0bd7a49b71cb96132e54c
|
||||
title: Build a Range of Numbers Generator
|
||||
challengeType: 26
|
||||
dashedName: build-a-range-of-numbers-generator
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
In this lab, you will build a `rangeOfNumbers` function that generates an array of numbers within a specified range.
|
||||
|
||||
Examples:
|
||||
|
||||
- `rangeOfNumbers(3, 9)` returns `[3, 4, 5, 6, 7, 8, 9]`
|
||||
- `rangeOfNumbers(5, 5)` returns `[5]`
|
||||
|
||||
Requirements:
|
||||
|
||||
- Use recursion (the function must call itself)
|
||||
- No loops or array methods (`for`, `while`, `forEach`, `map`, `filter`, `reduce`)
|
||||
|
||||
**Objective:** Fulfill the user stories below and get all the tests to pass to complete the lab.
|
||||
|
||||
**User Stories:**
|
||||
|
||||
1. You should create a function named `rangeOfNumbers` that takes two parameters: `startNum` and `endNum`.
|
||||
1. The function should return an array of integers that begins with the number represented by the `startNum` parameter and ends with the number represented by the `endNum` parameter (inclusive).
|
||||
1. The `startNum` will always be less than or equal to the `endNum`.
|
||||
1. Your function must use recursion by calling itself. It should not use any loop syntax (`for`, `while`, or higher-order functions such as `forEach`, `map`, `filter`, or `reduce`).
|
||||
1. The function should handle the base case where `startNum` equals `endNum` by returning an array containing just that single number.
|
||||
1. For the recursive case, the function should call itself with modified parameters to build the array, then add the current number to the result.
|
||||
1. The function should not rely on global variables to cache or build the array.
|
||||
|
||||
# --hints--
|
||||
|
||||
You should have a function named `rangeOfNumbers`.
|
||||
|
||||
```js
|
||||
assert.isFunction(rangeOfNumbers);
|
||||
```
|
||||
|
||||
The `rangeOfNumbers` function should take two parameters.
|
||||
|
||||
```js
|
||||
assert.match(rangeOfNumbers.toString(), /rangeOfNumbers\s*\(\s*\w+\s*,\s*\w+\s*\)/);
|
||||
```
|
||||
|
||||
Your function should return an array.
|
||||
|
||||
```js
|
||||
assert(Array.isArray(rangeOfNumbers(5, 10)));
|
||||
```
|
||||
|
||||
Your code should not use any loop syntax (`for` or `while` or higher order functions such as `forEach`, `map`, `filter`, or `reduce`).
|
||||
|
||||
```js
|
||||
assert(
|
||||
!__helpers.removeJSComments(code).match(/for|while|forEach|map|filter|reduce/g)
|
||||
);
|
||||
```
|
||||
|
||||
`rangeOfNumbers` should use recursion (call itself) to solve this challenge.
|
||||
|
||||
```js
|
||||
assert(
|
||||
rangeOfNumbers.toString().match(/rangeOfNumbers\s*\(.+\)/)
|
||||
);
|
||||
```
|
||||
|
||||
`rangeOfNumbers(1, 5)` should return `[1, 2, 3, 4, 5]`.
|
||||
|
||||
```js
|
||||
assert.deepStrictEqual(rangeOfNumbers(1, 5), [1, 2, 3, 4, 5]);
|
||||
```
|
||||
|
||||
`rangeOfNumbers(6, 9)` should return `[6, 7, 8, 9]`.
|
||||
|
||||
```js
|
||||
assert.deepStrictEqual(rangeOfNumbers(6, 9), [6, 7, 8, 9]);
|
||||
```
|
||||
|
||||
`rangeOfNumbers(4, 4)` should return `[4]`.
|
||||
|
||||
```js
|
||||
assert.deepStrictEqual(rangeOfNumbers(4, 4), [4]);
|
||||
```
|
||||
|
||||
`rangeOfNumbers(10, 15)` should return `[10, 11, 12, 13, 14, 15]`.
|
||||
|
||||
```js
|
||||
assert.deepStrictEqual(rangeOfNumbers(10, 15), [10, 11, 12, 13, 14, 15]);
|
||||
```
|
||||
|
||||
`rangeOfNumbers(2, 8)` should return `[2, 3, 4, 5, 6, 7, 8]`.
|
||||
|
||||
```js
|
||||
assert.deepStrictEqual(rangeOfNumbers(2, 8), [2, 3, 4, 5, 6, 7, 8]);
|
||||
```
|
||||
|
||||
Global variables should not be used to cache the array.
|
||||
|
||||
```js
|
||||
rangeOfNumbers(1, 3);
|
||||
assert.deepStrictEqual(rangeOfNumbers(6, 9), [6, 7, 8, 9]);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function rangeOfNumbers(startNum, endNum) {
|
||||
if (endNum - startNum === 0) {
|
||||
return [startNum];
|
||||
} else {
|
||||
const numbers = rangeOfNumbers(startNum, endNum - 1);
|
||||
numbers.push(endNum);
|
||||
return numbers;
|
||||
}
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"name": "Build a Range of Numbers Generator",
|
||||
"isUpcomingChange": false,
|
||||
"dashedName": "lab-range-of-numbers",
|
||||
"helpCategory": "JavaScript",
|
||||
"challengeOrder": [
|
||||
{
|
||||
"id": "5cc0bd7a49b71cb96132e54c",
|
||||
"title": "Build a Range of Numbers Generator"
|
||||
}
|
||||
],
|
||||
"blockLabel": "lab",
|
||||
"blockLayout": "link",
|
||||
"usesMultifileEditor": true
|
||||
}
|
||||
@@ -287,6 +287,7 @@
|
||||
"blocks": [
|
||||
"lecture-understanding-recursion-and-the-call-stack",
|
||||
"lab-countdown",
|
||||
"lab-range-of-numbers",
|
||||
"workshop-decimal-to-binary-converter",
|
||||
"lab-permutation-generator",
|
||||
"review-recursion",
|
||||
|
||||
Reference in New Issue
Block a user