JS Help with Recursion

Write a recursive function, sum(arr, n) , that returns the sum of the elements from 0 to n inclusive in an array arr .

function sum(arr, n) {
// Only change code below this line

// Only change code above this line
}

let res = 0;

function sum(arr, n) {
    if (n >= 0) {
  	    res += arr[n]
        sum(arr, n -1)
    }
}

sum([1, 2, 3], 1);
console.log(res)

this is what i do. Just adjust with your need

Harvard’s CS50 explains it pretty well. https://www.youtube.com/watch?v=mz6tAJMVmfM