This is their answers based on the code below of recursion, I dont get where they got all random arrays, they don’t have one particular array in this problem. I am confuse all these arrays, dont we suppose to have just one array. Can you please help to explain how they got the answers and what happen if we pass n = 1, or 2, 3, 4, 5, 6 and so on…
sum([1], 0)
should equal 0.
Passed
sum([2, 3, 4], 1)
should equal 2.
Passed
sum([2, 3, 4, 5], 3)
should equal 9.
Passed
Your code should not rely on any kind of loops ( for
or while
or higher order functions such as forEach
, map
, filter
, or reduce
.).
Passed
You should use recursion to solve this problem.
Your code so far
function sum(arr, n) {
// Only change code below this line
if (n <= 0) {
return 0;
} else {
return sum(arr, n - 1) + arr[n - 1];
}
// Only change code above this line
}
sum([1], 0);
sum([1], 1);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:81.0) Gecko/20100101 Firefox/81.0
.
Challenge: Replace Loops using Recursion
Link to the challenge: