Trying to use recursion functions for range creator

Tell us what’s happening:
:sob: What’s wrong with my brain? It just makes sense but unfortunatly only in my brains universe. :pleading_face:

Let me explain it step by step:

  1. First I create a global variable. (Not very good, I know, but it should work for this problem)
  2. Next there is the function declaration rangeOfNumbers(startNum, endNum) with two parameters. Let’s pretend to input rangeOfNumbers(1 ,3)
  3. 1 is smaller than 3, so we can immediately go to the else part
  4. take the global variable “range” which is by the way an array and push “1” inside it.
  5. than call the same function “recursevly” but add 1 to startNum befor
  6. rangeOfNumbers(2, 3) is being called, while the global variable “range” should have the value of [1]
  7. 2 is not bigger than 3, so we can immediatly go to the else part again
  8. take the “range” variable and push 2 into it. “range” variable should now hold the values [1 ,2] as an array.
  9. call the same function again rangeOfNumbers(2+1, 3)
  10. …etc…
  11. after one more loop we are getting that 4 is bigger than 3 and the if statement is true
  12. the function should now return the “range” [1, 2, 3] and as its is a return, the function should stop executing.

I guess, I need a little break for now. I feel like somethink is wrong with JavaScript or with me, this is more likely. My notes for today, Recursion functions are evil. :smiling_imp:

Your code so far


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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36 OPR/71.0.3770.323.

Challenge: Use Recursion to Create a Range of Numbers

Link to the challenge:

The global variable will not work. The global variable approach looks like recursion, but it isn’t true recursion.


Your code contains global variables that are changed each time the function is run. This means that after each test completes, subsequent tests start with the previous value. To fix this, make sure your function doesn’t change any global variables, and declare/assign variables within the function if they need to be changed.

Example:

var myGlobal = [1];
function returnGlobal(arg) {
  myGlobal.push(arg);
  return myGlobal;
} // unreliable - array gets longer each time the function is run

function returnLocal(arg) {
  var myLocal = [1];
  myLocal.push(arg);
  return myLocal;
} // reliable - always returns an array of length 2
1 Like

Oh there is this return statement missing? :man_facepalming:
Whatever I don’t undersand it but it is working. I guess, this is coding…isn’t it? :man_shrugging:
Well, now I can take a break. :slight_smile:

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

This still has the global variable, so this only works once.

1 Like

Oh thank you so much Jeremy. You were very fast with your response. :slight_smile:
Yes honestly, this with the global variable is somethink I’m aware of. ^^ :shushing_face:
But I thought it would be enought for this challenge. XD
I was confused because the test results never told me that there is an array given as a result. So the if Statement was never executed or what? Idk I’m still confused…
But thanks for your solution, I will keep it in mind. ;D

Your function should always return an array. rangeOfNumbers(a, b) should always return [a, a+1, ..., b], even when you call the function inside of itself (with a smaller range).

The idea behind recursion is to call the function inside of itself with reduced inputs and modify that result before returning it.

1 Like

Oh ok. I think i got it a little bit more. Somehow I thought I wouldn’t be necessary to return something when the calculation isn’t ready yet. But I will keep in mind know, to always return at least something. :smile:
Thank you for explaining me this. :blush:

You’re welcome; it’s no trouble at all.

Recursion is very tricky, so feel free to ask more questions as they come up.

1 Like

You can’t have global variables, all arguments have to be passed in as parameters.

So a function with three parameters rather than two will work.

function rangeOfNumbers(startNum, endNum, range = []) {
  if (startNum > endNum) {
    return range;
  }
  // .....snip

As will not using a global variable

function rangeOfNumbers(startNum, endNum) {
  let range = [];

  function loop () {
    if (startNum > endNum) {
      return range;
    }
    // .....snip
  }

  return loop();
}
1 Like

I like the inner/outer function approach to scope the global variable and the extra argument to pass around the array. I think it’s important to be able to understand using the return values for recursion though, because it solidifies a lot of knowledge about how variables, function calls, scope, and return values work.

The extra argument is a favorite of mine. Can’t do that in C as cleanly.

2 Likes