Tell us what’s happening: What’s wrong with my brain? It just makes sense but unfortunatly only in my brains universe.
Let me explain it step by step:
First I create a global variable. (Not very good, I know, but it should work for this problem)
Next there is the function declaration rangeOfNumbers(startNum, endNum) with two parameters. Let’s pretend to input rangeOfNumbers(1 ,3)
1 is smaller than 3, so we can immediately go to the else part
take the global variable “range” which is by the way an array and push “1” inside it.
than call the same function “recursevly” but add 1 to startNum befor
rangeOfNumbers(2, 3) is being called, while the global variable “range” should have the value of [1]
2 is not bigger than 3, so we can immediatly go to the else part again
take the “range” variable and push 2 into it. “range” variable should now hold the values [1 ,2] as an array.
call the same function again rangeOfNumbers(2+1, 3)
…etc…
after one more loop we are getting that 4 is bigger than 3 and the if statement is true
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.
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
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
Oh there is this return statement missing?
Whatever I don’t undersand it but it is working. I guess, this is coding…isn’t it?
Well, now I can take a break.
var range = [];
function rangeOfNumbers(startNum, endNum) {
if (startNum > endNum) {
return range;
}
else {
range.push(startNum);
rangeOfNumbers(startNum + 1, endNum);
return range;
}
};
Oh thank you so much Jeremy. You were very fast with your response.
Yes honestly, this with the global variable is somethink I’m aware of. ^^
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.
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.
Thank you for explaining me this.
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.