Help for Replace Loops using Recursion

I’m into this challenge: Replace Loops using Recursion in Basic JavaScript
The code example in the challenge is this:

function multiply(arr, n) {
    if (n <= 0) {
      return 1;
    } else {
      return multiply(arr, n - 1) * arr[n - 1];
    }
  }

And I’ve coded it like this:

let arr = [1, 2, 3, 4]; 

function multiply(arr, n) {
    if (n <= 0) {
        return arr[0];
    } else {
        return arr[n] * multiply(arr, n - 1);
    }
}

multiply(arr, arr.length - 1);

Both codes return the same value. I really wonder if there’s any subtle difference between these two?

Link to the challenge:

try with an array that doesn’t start with 1

example, change use an array like let arr = [3,2,4,5], and see if you still have the same exact result

1 Like