Basic JavaScript: Understanding Recursion Example

In the “Basic JavaScript: Replace Loops using Recursion” I have a hard time to understand well the logic behind this code:

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

For what I know is that this function multiply the numbers in the arrays starting from the first one to the n number. Example: multiply([1,2,3,4], 3) this will multiply 1 x 2 x 3 which result in 6. What I don’t understand is the

else {
    return multiply(arr, n - 1) * arr[n - 1];
  }

part. I don’t know how the multiplication in this line works and I don’t know why it would loop. Please can someone just explain me step by step what happens in this line code.

Welcome, olivier.

I moved your question to its own topic because you were asking a question related to your own code for a challenge and were not answering the OP of the other thread. It is always best to create your own thread for you specific question(s). Also, it is advisable to use the Ask for Help button on the challenge, so it auto-populates with your current code and the challenge url.


I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

Thank you.

As for your question: You have this function

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

You would use this function by calling it like so (this is an example):

var myArr = [2, 3, 4, 5];
var numberOfElements = 2;
multiply(myArr, numberOfElements);

When this is called, the following will happen:

function multiply(arr, n) {
// Now arr = [2, 3, 4, 5], and n = 2
// n > 0, So this does not run
    if (n <= 0) {
      return 1;
    } else { // The function is called again, but the arguments have changed
      return multiply(arr, n - 1) * arr[n - 1]; // arr[2-1] is the same as arr[1] == 3 (the second element of arr)
    }
  }

Next, this:

function multiply(arr, n) {
// arr = [2, 3, 4, 5];
// n = 2-1
    if (n <= 0) { // This still does not apply
      return 1;
    } else { // This is called again, again with different arguments
      return multiply(arr, n - 1) * arr[n - 1]; // arr[1-1] == arr[0] which is equal to 2 (the first element of arr)
    }
  }

Next this:

function multiply(arr, n) {
// arr does not change...
// n = 2 - 1 - 1 which is equal to 0
    if (n <= 0) { // This is true!
      return 1; // So, 1 is returned...
    } else {
      return multiply(arr, n - 1) * arr[n - 1];
    }
  }

Now, follow everything backwards to get to the original return statement.

I hope this helps. If not, search this forum for posts about recursion, as others have answered with much better responses.