Reputation: 283
I came across this problem on FreeCodeCamp.org (link to the problem below) and I was wondering if someone could help me better understand why this equals 2 when calling with sum([2, 3, 4], 1); I sat and looked at this for a bit but just feel blocked mentally in understanding how this works. Simple developer trying to understand Recursion in Javascript.
Any help would be much appreciated! Thanks internet!
function sum(arr, n) {
if(n <= 0){
return 0;
}else {
return sum(arr, n - 1) + arr[n - 1];
}
}
sum([2, 3, 4], 1) // Returns 2
Here is a link to the problem: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/replace-loops-using-recursion
Upvotes: 0
Views: 3660
Reputation: 1291
sum(arr, 1-1)
evaluates to 0 because of the if statement that checks if n
is <=0
and
arr[n-1]
is equal to 2 as you are essentially trying to access the arr[1-1]
which is arr[0]
.
Therefore, it is returning 2 because return sum(arr, n-1) + arr[n-1];
evaluates to 2.
Upvotes: 0
Reputation: 1097
sum(arr, n)
is a recursive function that returns the sum of the first n
elements of an array arr
.
In your example, you provide sum([2, 3, 4], 1)
which basically says, compute the sum of the first element (i.e. the value of n
in this example is 1
).
So it would go through the function as such...
// the first time through
function sum([2, 3, 4], 1) {
if(1 <= 0){ // false this time
return 0;
}else { // this is where we end up
return sum([2, 3, 4], 0) + 2; // sum will be the result of the recurse back into the function, plus 2
}
}
// the second time through
function sum([2, 3, 4], 0) {
if(0 <= 0){ // true this time
return 0; // send this result back up to the first run through
}else {
// not relevant this time
}
}
// back in the the first time through,
// we now have a value to work with below
// remember, this isn't the 'third' time through,
// it is back in the first time run through
// just re-printed here so you could see
// where the value gets returned from the second run
function sum([2, 3, 4], 1) {
if(1 <= 0){ // false this time
return 0;
}else { // this is where we end up
return sum(0 + 2); // we got a result from the second run through, sum is now 2
}
}
Upvotes: 1
Reputation: 25332
Actually, with n
equals to 1
is quite easy. Let's check step by step. Here your sum
function; let's add some line to make it easier to refer:
1: function sum(arr, n) {
2: if(n <= 0){
3: return 0;
4: }else {
5: return sum(arr, n - 1) + arr[n - 1];
6: }
7: }
Now, let's see what's happening step by step when you execute:
sum([2, 3, 4], 1)
The function sum
is called with arr
equals to [2, 3, 4]
, and n
equals to 1
.
Since n
is not less or equals than 0 (line 2), we go to the else
block, at line 5
.
Now, here is where the recursion happens: we call again the function sum
, passing the same arr
, but not the same n
, instead we pass n - 1
.
So we call sum
again, this time with arr
equals to [2, 3, 4]
but with n
equals to 0
.
Since n
is 0
this time, at the line 2 check we proceed to line 3 and returns 0
.
Now, the function exit, with the value 0
, that we gave to the caller.
And the caller of sum([2, 3, 4], 0)
was the execution sum([2, 3, 4], 1)
, specifically at line 5:
5: return sum(arr, n - 1) + arr[n - 1];
Since it returned 0
, we can imaging like:
5: return 0 + arr[n - 1];
And remember that n
is 1
, so:
5: return 0 + arr[0];
Since arr[0]
is equals to 2
:
5: return 0 + 2;
And then why sum([2, 3, 4], 1)
returns 2
.
I'm not sure if it's clearer now, but I hope so. :)
Upvotes: 5