Continuing the discussion from freeCodeCamp Challenge Guide: Replace Loops using Recursion:
Hello,
so this it my solution for this challenge:
function sum(arr, n) {
// Only change code below this line
if (n > 0 && n <= arr.length) {
return sum(arr, n-1) + arr[n-1];
} else if (n > arr.length){
return "There are only " + arr.length + " items in array!";
} else {return 0;}
// Only change code above this line
}
console.log(sum([1, 3, 7], 3));
It’s working and pass the test, but I am wondering, if it’s 100% correct?