0

I been working on this problem that asks to find the values within the two given inputs. For instance, input: (1,5) yields the result: [1,2,3,4,5]. This problem should give a solution that is done recursively.

Here is the solution that works:

function rangeOfNumbers(startNum, endNum) {
  if (endNum - startNum === 0) {
    return [startNum];
  } else {
    var numbers = rangeOfNumbers(startNum, endNum - 1);
    numbers.push(endNum);
    console.log(numbers); 
    return numbers;
  }
}

The trouble that I am having is understanding how this solution works. I tried debugging through console.log and it looks like values are added like this:

[ 1, 2 ]
[ 1, 2, 3 ]
[ 1, 2, 3, 4 ]
[ 1, 2, 3, 4, 5 ]

If my endNum value is declared as 5 initially and I'm pushing endNum to the end of the array, why doesn't my array look like this: [ 5, 4, 3, 2, 1 ]

What's surprising was when I changed my recursive function to this:

var numbers = rangeOfNumbers(startNum+1, endNum);
numbers.push(startNum);

The array looked like this:

[ 5, 4, 3, 2, 1 ]

Any feedback or clarification on this would be greatly appreciated, thanks!

9
  • 2
    With the first example, your base case (innermost call to rangeOfNumbers()) is startNum = 1, endNum = 1. After your modification, the base case becomes startNum = 5, endNum = 5. Commented Dec 11, 2019 at 0:21
  • You don't push endNum into the array until after you have constructed the array with all the lower values of endNum in it first. So you end up pushing 1 (when endNum - startNum === 0), then 2, then 3, then 4 and finally 5. Commented Dec 11, 2019 at 0:26
  • numbers always starts as [startNum] and the call just before the one that returns [startNum] is the call that has endNum set to 2, in the one before that it is 3 and so on. Commented Dec 11, 2019 at 0:29
  • @Nick I partially understand your comment. If nothing is pushed until I have constructed the array with all the lower values of endNum in it first. How do the statements execute? Is it something like this: 1. rangeOfNumbers(1, 5 - 1) 2. rangeOfNumbers(1, 4 - 1) 3. rangeOfNumbers(1, 3 - 1) 4. rangeOfNumbers(1, 2 - 1) 5. rangeOfNumbers(1, 1 - 1) And then my values are pushed starting from statement 5? Commented Dec 11, 2019 at 1:08
  • 1
    @traveler316 yes, that's it. As the recursion unrolls, those results get added, so it's in the reverse order to the calls, which is why the final array comes out as [1,2,3,4,5] Commented Dec 11, 2019 at 2:38

1 Answer 1

1

basically what it does is the following:

1) check that number(endNum) is equals to startNum. (this is the breaker condition) 2) if they are not equals, it will call the function rangeOfNumbers with an endNum reduced by 1.

now lets take an example of rangeOfNumbers(1,3) so it is smaller.

lets call each return number#X so we now what we are assigning

1) rangeOfNumbers(1,3).

start is not equal to end (1!==3), therefore we call again rangeOfNumbers and assign it to the variable numbers(number#1) and the end is reduced by one.

2)rangeOfNumbers(1,2).

start is not equal to end(1!==3), therefore we call again rangeOfNumbers and assign it to the variable numbers(number#2) and the end is reduced by one.

3) rangeOfNumbers(1,1).

start is equal to end(1 === 1), therefore we return [1] (startNum)

4) basically we assign this value to number#2 and then we push endNum (from step 2 ;)), so we do:

// equal to do [1].push(2) because endNum in step2 was 2.
numbers.push(endNum);
return numbers;

5) on this step we assign to number#1 the returned value from step 4, which is [1, 2] and we do the same that on step 4;

// equal to do [1, 2].push(3) because endNum in step1 was 3.
numbers.push(endNum);
return numbers;

6) we return that last return, so end of the function.

[1, 2, 3]

in other form to see it, it could be something like

rangeOfNumbers(1,1) --> this returns [1]
rangeOfNumbers(1,2) --> this returns [1, 2]
rangeOfNumbers(1,3) --> this returns [1, 2, 3]
console.log(result)

this is read from the bottom to the top, like a LIFO

read this article, explains everything in detail with images

answering your question on why: var numbers = rangeOfNumbers(startNum+1, endNum); makes your array to be [5, 4, 3, 2, 1] it is because you are rising your start from 1 to 5, and then you start pushing. if you read my previous answer, it will be clear for you to discover why.

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


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

Sign up to request clarification or add additional context in comments.

2 Comments

In steps 1-3, is it end that is assigned to numbers? or is it start that is assigned to the return statement: number#X?
sorry @traveler316 you are correct, it is start but remember that it doesn't matter, because startNum === endNumb

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.