Replace Loops using Recursion - Explained

Hey all,

I wanted to share this, as I read through the previous posts and nothing mentioned my problem with understanding how this all works.


Here is my code:

function sum(arr, n) {

// Only change code below this line

if (n <= 0) {

return 0;

} else {

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

For anyone still stuck or for those who come across this looking for clarity, here is a breakdown of what is happening:

First, let’s break this down from the beginning.

function sum(arr, n) {

  • The arr is an array that the user will be inputting with the function.
  • The n is, and this is important, the number of elements we are asking to sum in arr.
  • This means that whatever n is, will refer to the number of elements we want to sum in the array.

if (n <= 0) {

return 0;
  • This is saying that if the value of n is less than or equal to 0, return the value of 0.

else {

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

This was the tricky part for me, so I will break this into parts:

return sum(arr, n - 1)

  • This is calling the function of summing the array within the number of elements, n, assigned. So for example, if n = 1, this will set n - 1 to equal 0, meaning that 0 elements will be summed from this portion of the equation. If n = 3, the value of n - 1 would be 2, so the first 2 elements in arr will be summed.

arr[n - 1]

  • This just refers to the index of the array at the value of n - 1. So for the example, if n = 1, the value of this equation would be 0, so we would use the index of 0 in the array. If n = 3, the value of this equation would be 2, so we would use the index of 2 in the array.

Put this together and you get a function that sums the elements in arr. First, it checks to make sure that n will not equal 0. Then it firsts gathers the number of elements to sum, then it adds that sum to the final index value of whatever n - 1 is. My problem was assuming n was the index, and I kept confusing myself.

Hope this helps.


Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:92.0) Gecko/20100101 Firefox/92.0

Challenge: Replace Loops using Recursion

Link to the challenge:

2 Likes

Wow! Extremely helpful! Thanks! :grinning_face_with_smiling_eyes:

No worries. Glad it helped :slight_smile:

Be very careful here. There is no ‘going back’ or GOTO in this recursion. You are calling the function all over again and this new function call only knows about the two arguments you passed in. There is no ‘go back’ to the other if case. This makes a brand new function call.

Thank you for the clarification.

So, would it be more accurate to describe this as: “if n = 1, this will set n - 1 equal to 0 and and since the value of 0 has now been set, the function makes a new call and runs the value of 0 from the beginning of the code, which would return 0.”

Not really?

I would say ‘If n is greater than 0, then we make a new function call to sum with the same arr but with n - 1 as the second argument instead of n. In other words, if n is greater than 0, then we call sum again (recursively) to get the sum of the first n-1 elements in the array.’

1 Like

I was just overthinking it. I edited to make it more clear and not use incorrect wording. Let me know if it is still incorrect.

If we want to get real picky here then this is not technically correct. The number of elements in the array is always the same because we never change the array. So if there are five element in the array then each time we recursively call the function there will still be five elements in the array. I guess you could say that n represents the number of elements we want to pretend are in the array. It’s a self-imposed limit. If n is 2 then that means we want to sum the first two elements in the array and thus we pretend there are only two elements. But in reality the array still contains the original five elements.

2 Likes

Yes, thank you. I edited that for more clarity.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.