Trying to solve "Use Recursion to Create a Range of Numbers" challenge

What are the missing or mistakes on my code?:

Here is my code so far


function rangeOfNumbers(startNum, endNum) {
if (startNum === endNum) {
  return [endNum];
}
if (endNum - 1 === startNum) {
  return startNum;
}
var countRange = [endNum - 1];
countRange.push(endNum);
countRange.unshift(startNum);
return countRange;
}
console.log(rangeOfNumbers(1, 5));











Challenge: Use Recursion to Create a Range of Numbers

Link to the challenge:

You aren’t currently using recursion. There is no call to rangeOfNumbers inside rangeOfNumbers.

how to call recursion?
Could you help me more?
Thank you :slightly_smiling_face:

Hey @abdulrahman.mhd.anas!

Here are some resources that can help with your understanding on recursion.

Hope that helps!

1 Like

Recursion is introduced and explained in this lesson:

I still don’t fully understand recursion, but I didn’t understand recursion at all until I opened up some recursive code in Chrome Dev Tools.

SO:

  1. Open a new tab in Chrome and enter about:blank in the address bar.

  2. Ctrl+Shift+I opens Dev Tools. It’ll probably open on the Elements tab, so click on the Sources tab.

  3. You should see something like ‘Snippets’ and ‘New snippet’. (sry not more detailed but for me this is permanently open in a tab somewhere). Make a new snippet.

  4. This is a fantastic way to test and debug code. Plug this is in there:

function countdown(n){
debugger
  if(n < 1) {
    return [];
  } else {
    let arr = countdown(n - 1);
    arr.unshift(n);
    return arr;
  }
}

countdown(5);

Notice ‘debugger’? That’s what allows the magic to happen.

  1. Hit Ctrl+Enter to run the code…okay it’s paused.

  2. Over to the side you should see some controls like a play button, etc. There’s a section showing you the current scope, console at the bottom, yadda yadda. There’re videos that go into more detail but, for now, just click the button that looks like an arrow pointing at a circle.

What this does is walks through the code one. step. at. a . time. So you can always see what’s happening.

So what’s recursion? It’s a function that calls itself. But I prefer to think of it as a function that recedes into itself, almost as if each function call is it’s own separate room and the program is walking from room to room; this is kinda how it works. If you run that code one step at a time, you might notice a few things.

  1. n is reduced by 1 with each function call. Where are the numbers going? They’re getting left behind in each function scope (or “room”) for later. Like breadcrumbs. Kinda.

2)The conditional statement if(n < 1) { return []; } is like the final room. Once n < 1 the program picks up an empty array (return []) , turns around, and goes back through each room (function call).

  1. On it’s way back, it’s executing everything in each room. It picks up the n it left behind and puts it in its little box arr.unshift(n); and brings it into the next room return arr; Until it’s done.

Hopefully, this metaphor helps and is not more confusing than it needs to be. Best of luck!

3 Likes

Simply put recursion is like calling a function inside itself.

Thanks to everyone that helped me. you all helped and I appreciate that.
Thank you everyone :slight_smile:
MY code so far :point_down::

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