Basic JavaScript - Replace Loops using Recursion

Tell us what’s happening:
Describe your issue in detail here.
I understand the lesson and know how to solve the problem BUT
I have a question about the set up in the example given.
originally it was written like this function multiply(arr, n) { if (n <= 0) { return 1; } else { return multiply(arr, n - 1) * arr[n - 1]; } }
why return 1 if n<=0? that makes the function give an incorrect answer when you evaluate at n=0, because index starts at 0.
so evaluating multiply([5,2,3],0) would return 1, when in should return 5, since 5 is in the 0th position and there’s nothing to multiply it by.

so why would we ever wanna return 1?
shouldnt it be written like i did below?
when u run the function that way multiply([5,2,3],0) now gives 5. when n is 1, it returns 10 etc.

Your code so far

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

  // Only change code above this line
function multiply(arr, n) {
    if (n <= 0) {
      return arr[0];
    } else {
      return multiply(arr, n - 1) * arr[n];
    }
  }console.log(multiply([5,2,3],0))

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Replace Loops using Recursion

Link to the challenge:

The second param is supposed to tell you how many elements to multiply.

So a zero should give 0 according to the test cases

test :sum([1], 0) should equal 0.

(In your current code it will return 1)

i dont understand what you are saying. ignore the Sum function please, i just copied the example code to the bottom of the FCC console to test it out.
im asking about the multiply function. thanks

Yes I got confused and thought you were showing the code for sum. So yes what I said doesn’t count.

For the multiply function, the base case is 1 because the objective is to multiply all the numbers. They are assuming that the valid domain for n is 0+ meaning that they have defined their function to give back 1 if you give n=0, but to give back the product of the first element and 1 if n is 1 (and the product of the first and second elements times 1 if n is 2 and so on)

Edit: In your version, you have changed that definition to be: return the first element for n=0, return the product of the first element and the second element again for n equal 1, etc.

ok thanks man i get it. I really appreciate the explanation

1 Like

You are considering n as the index that is being acessed in the moment, but the function consider n as the lenght of the array. So, the array [5,4,2] has n=3, because his length is 3.

Look, when the original function will return the multply, it uses multiply(arr, n-1) * arr[n-1], why use arr[n-1]? Because the array begins with 0.

That’s why we return 1 when n=0, because when n=1 we will access the arr[1 - 1] => arr[0]! So, when n=0, we will access the arr[-1], that’s impossible! So we use the neutral number in multiply operation (number 1) to stop our recursion.