skip to Main Content

Here is the prompt:

We have defined a function named rangeOfNumbers with two parameters. The function should return an array of integers which begins with a number represented by the startNum parameter and ends with a number represented by the endNum parameter. The starting number will always be less than or equal to the ending number. Your function must use recursion by calling itself and not use loops of any kind. It should also work for cases where both startNum and endNum are the same.

my attempt:

function rangeOfNumbers(startNum, endNum) {
  if (startNum > endNum) {
    return [];
  } else {
    const array = rangeOfNumbers(startNum + 1, endNum);
    array.push(startNum);
    return array;
  }
}

console.log(rangeOfNumbers(1, 5));

(Wrong)


given solution:

function rangeOfNumbers(startNum, endNum) {
  if (endNum < startNum) {
    return [];
  } else {
    const numbers = rangeOfNumbers(startNum, endNum - 1);
    numbers.push(endNum);
    return numbers;
  }
}

console.log(rangeOfNumbers(1, 5));

(Right)

Why do they not produce the same results?

2

Answers


  1. I think you just do not understand very well about the Running sequence of recursion and this is a very small bug in your code, so let’s think differently,if we want to know rangeOfNumbers(1,5),we should known rangeOfNumbers(2,5) before,and if we want to know rangeOfNumbers(2,5),we should known rangeOfNumbers(3,5) before,until rangeOfNumbers(6,5),we know the result is [],
    we got a pattern,

    rangeOfNumbers(6, 5); // []
    rangeOfNumbers(5, 5) = rangeOfNumbers(6, 5).push(5) //  [5]
    rangeOfNumbers(4, 5) = rangeOfNumbers(5, 5).push(4) //  [5,4]
    rangeOfNumbers(3, 5) = rangeOfNumbers(5, 5).push(3) //  [5,4,3]
    rangeOfNumbers(2, 5) = rangeOfNumbers(5, 5).push(2) //  [5,4,3,2]
    
    // so,I think we could solve your problems just use unshift instead of push 
    function rangeOfNumbers(startNum, endNum) {
      if (startNum > endNum) {
        return [];
      } else {
        const array = rangeOfNumbers(startNum + 1, endNum);
        // array.push(startNum);
        array.unshift(startNum);
        return array;
      }
    }
    console.log(rangeOfNumbers(1, 5));
    Login or Signup to reply.
  2. The algorithm’s execution can be envisioned as the conversion of a formal definition range(start, end) of an array into its practical implementation.

    Through recursion: range(start, end) -> range(start + 1, end) you eliminate start from the beginning of the formal definition of the array.

    When invoking: array.push(start), you add the removed start to the end of the actual array. The method push(item) does indeed append the item to the end (the right extremity) of the array. This constitutes an error. If an index is removed from the beginning of the formal definition, it should be appended to the beginning of the actual array implementation.

    The correct solution addresses this issue by appending the item removed from the end of the formal definition of the array range(start, end) -> range(start, end - 1) to the end of the actual array array.push(end).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search