Question about Replace Loops using Recursion challenge

I am confused because I don’t understand why do we need to pass array as argument here, when it works same if we only pass n, which makes it simpler for me to understand.

const arr = [2,3,4,5]

function sum(n) {
  if(n <= 0) {
    return 0;
  } else {
    return sum(n - 1) + arr[n - 1];

The main reason is such function would depend on the arr variable from the outer scope. This makes it less usable, and requires additional care to be able to use it with various arrays.

When array is passed as one of the arguments, function doesn’t care what is the variable name of the array outside of the function. It has everything it needs passed in.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.