I need help with this recursion challenge

Tell us what’s happening:

Hello, I’m still learning recursion i don’t know what exactly is happening i don’t know what i have to do in this challenge… what is the goal?

Your code so far


function sum(arr, n) {
// Only change code below this line
   
  // return sum(arr, n - 1) * arr[n];
// 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/79.0.3945.117 Safari/537.36.

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

The challenge statement is: " Write a recursive function, sum(arr, n) , that returns the sum of the elements from 0 to n inclusive in an array arr ."

One of the tests is: " sum([2, 3, 4], 1) should equal 5." (arr[0]+arr[1]=2+3=5)

sum([2, 3, 4], 2) should be 9 (2+3+4)

1 Like

try to understand the example code, what do you not understand of that one?

1 Like

Thank you i think i understood whats happening
it took a long time really!

1 Like

i was trying to understand whats happening it looks like i know how it works now i just need to clear some doubts