Help me recursion

can someone explain the recursive function better?
like what does n represent?

as i understand + arr[n - 1] is that saying to keep going down/iterate through the values within the array and add it to the recusive function, which will make it stack as i understand(not sure), i’m also a bit confuse on the part where the calls are being stacked that also one of the reasons i don’t understand the input

sum([1], 0) should equal 0.
sum([2, 3, 4], 1) should equal 2.
sum([2, 3, 4, 5], 3) should equal 9.

  **Your code so far**

function sum(arr, n) {
// Only change code below this line
if(n <= 0){
  return 0;
}else{
 return sum(arr, n - 1) + arr[n - 1];
}

// Only change code above this line
}
  **Your browser information:**

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

Challenge: Replace Loops using Recursion

Link to the challenge:

When ever you call a function it gets added to the stack and the stack follows the principal of FILO (First in Last Out) or LIFO (Last In First Out). What is essentially happening here is that you have a base case and once this base case is met (if it is not met you end up with infinite recursion) the stack collapse and thats gonna look a bit a like this: Lets say we have an array of [1,2,3] and we call the function such that sum([1,2,3] , 2) as you can see the function will then recurse 2 times and in this looks like callback + 2 and callback + 1 are added to the stack in that order and you will noticed that the first call is on the bottom of the stack and when it collapses it will enter from the top with the return value of the base case and this will look like 0 + 1 then 1 + 2 and the sum will be 3

So point of the function is to add up the first n numbers in an array:

Write a recursive function, sum(arr, n), that returns the sum of the first n elements of an array arr.


I’ll take one of the test cases as my example

sum([2, 3, 4, 5], 3) should equal 9.

It is saying “add up the first three numbers in that array”.

So n is 3, and arr is [2,3,4,5].

To understand what’s happening, start with the base case, the point at which the function stops and returns a concrete value:

if(n <= 0){
  return 0;
}

So sum([2, 3, 4, 5], 0) is just 0.

What if n is 1?

} else {
  return sum(arr, n - 1) + arr[n - 1];
}

So substitute in arr and 1 for n:

} else {
  return sum([2,3,4,5], 1 - 1) + [2,3,4,5][1 - 1];
}

That’s

} else {
  return sum([2,3,4,5], 0) + [2,3,4,5][0];
}

You already know sum([2,3,4,5], 0) is just 0, so:

} else {
  return 0 + 2;
}

So sum([2,3,4,5], 1) is 2


What if n is 2?

} else {
  return sum([2,3,4,5], 2 - 1) + [2,3,4,5][2 - 1];
}

That’s

} else {
  return sum([2,3,4,5], 1) + [2,3,4,5][1];
}

You now know sum([2,3,4,5], 1) is 2, so:

} else {
  return 2 + 3;
}

So sum([2,3,4,5], 2) is 5


What if n is 3?

} else {
  return sum([2,3,4,5], 3 - 1) + [2,3,4,5][3 - 1];
}

That’s

} else {
  return sum([2,3,4,5], 2) + [2,3,4,5][2];
}

You now know sum([2,3,4,5], 2) is 5, so:

} else {
  return 5 + 4;
}

So sum([2,3,4,5], 2) is 9


This is going backwards, but that’s what the recursive function is doing. Until n equals 0, the function keeps calling itself with n decremented by one. Once that 0 is reached, we can “unwind” back up, resolving each step, as described above

1 Like

this is similar to what everyone said before:

like algebra:

//return arr[3-1]+ sum([2,3,4,5], 3-1)
  //return arr[2-1]+ sum([2,3,4,5],2-1)
    //return arr[1-1] + sum([2,3,4,5],1-1)
     // return 0

so everytime replace that call with everything it returns:

//return arr[3-1]+
  //return arr[2-1]+ sum([2,3,4,5],2-1)
    //return arr[1-1] + sum([2,3,4,5],1-1)
     // return 0

and…

//return arr[3-1]+
  //return arr[2-1]+ 
    //return arr[1-1] + sum([2,3,4,5],1-1)
     // return 0

and…

//return arr[3-1]+
  //return arr[2-1]+ 
    //return arr[1-1] + 
     // return 0

and…

//return arr[2]+
  //return arr[1]+ 
    //return arr[0] + 
     // 0

and…

//return arr[2]+
  //return arr[1]+ 
    //return arr[0] + 0

and…

//return arr[2]+
  //return arr[1]+ arr[0] + 0

and…

//return arr[2]+arr[1]+ arr[0] + 0
1 Like

thanks man you explain really well

1 Like