Replace Loops using Recursion - Why is the answer 5!? Please help

Tell us what’s happening:
I have spent a full day and nearly all night trying to figure out how this algorithm works.

I just need a textual breakdown of how this algorithm outputs 5, for me 6 makes sense as it’s every number in the array -1 then added together, so 2-1, 3-1, 4-1, so 1+2+3. But that clearly isn’t the case, some one please help, I’m close to defeat!

Your code so far


function sum(arr, n) {
// Only change code below this line
if (n <= 0) {
  return arr[0];
} else {
  return sum(arr, n - 1) + arr[n];
}
// Only change code above this line
}
var total = sum([2, 3, 4], 1);
console.log(total)

Your browser information:

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

Challenge: Replace Loops using Recursion

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/replace-loops-using-recursion

1 Like

it is asking to sum the numbers till index 1 in the array, so to sum arr[0] and arr[1]

notice that n is always only used as array index, so it is the array index that is decreased not the value in the array

1 Like

n is the index of the highest value you should sum. 2+3 is 5.

1 Like

Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. In the future, if you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

Thank you.

Thankyou so much for your reply, it was really helpful. Could you please review my comments at the bottom of my code here and tell me if I am understanding this correctly…?
Sorry about the spoiler, I have [spoiler] commented out my reply!
Thanks again.

function sum(arr, n) {
// Only change code below this line
if (n <= 0) {
return arr[0];
} else {
return sum(arr, n-1) + arr[n];
}
// Only change code above this line
}
var total = sum([2, 3, 4], 1);
console.log(total)

/* So…
We pass ‘1’ into the function sum([2, 3, 4], “1”<-)
Because it is not <= 0, the function returns the array (‘arr’), decrements ‘n’/index by 1 (meaning it is now 0). But before returning, asks the function to add the contents of ‘arr’ together (+ arr[n]), up until the index specified index [n], 1 in this case?

So it would look kind of like this,

sum([2,3,4],1)
sum([0],0) + [2+3] = 5

Feel free to make any corrections to my workings at here to give a more acurate depiction of what happens within the function. Thankyou so much…
*/

Thankyou very much ArielLeslie, both yours and Ieahleen’s comments helped to to give me a better understanding! Good work!

the function doesn’t return arr

"recursion breakdown

the function sum([2,3,4], 1) is called
n is higher than 0 so this function returns the sum of an other function and a number, but n is decreased by 1: sum([2,3,4], 0) + 3
the function is called, this time n is 0 so the function returns arr[0], which is substituted instead of the function call
so sum([2,3,4], 0) becomes 2
and sum([2,3,4], 0) + 3 becomes 2+3 so 5

1 Like

Thankyou so much, really appreciate the help!