Back to Freecodecamp

Build a Range of Numbers Generator

curriculum/challenges/english/blocks/lab-range-of-numbers/5cc0bd7a49b71cb96132e54c.md

latest3.3 KB
Original Source

--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, Array.from, 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.
  2. 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).
  3. The startNum will always be less than or equal to the endNum.
  4. Your function must use recursion by calling itself. It should not use any loop syntax (for, while, Array.from(), or higher-order functions such as forEach, map, filter, or reduce).
  5. The function should handle the base case where startNum equals endNum by returning an array containing just that single number.
  6. For the recursive case, the function should call itself with modified parameters to build the array, then add the current number to the result.
  7. 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, while, Array.from() or higher order functions such as forEach, map, filter, or reduce).

js
assert(
  !__helpers.removeJSComments(code).match(/for|while|forEach|map|filter|reduce|Array\.from/g)
);

rangeOfNumbers should use recursion (call itself) to solve this challenge.

js
assert.isAtLeast(
  __helpers.removeJSComments(rangeOfNumbers.toString()).match(/rangeOfNumbers\s*\(.+\)/g)?.length,
  2
);

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;
  }
}