# Basic JavaScript - Replace Loops using Recursion

Can someone please help me with this question. I don’t think I understand the question at all.

I saw someone in another thread break down the question but it has since closed. they used the below formula as a way to explain. Could someone explain the `return arr[n - 1] * multiply(arr, n - 1) part to me? I dont understand why I have to multiply the array by itself?

``````var myArr = [1,2,3,4]

function multiply(arr, n) {

if (n <= 0) {

return 1;

} else {

return arr[n - 1] * multiply(arr, n - 1); // look at this

}
}

console.log(multiply(myArr, 4))
``````

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

Challenge: Basic JavaScript - Replace Loops using Recursion

here’s a link to that thread HOW is `multiply(arr, n) == multiply (arr, n - 1) * arr[n - 1]`? - #6 by Jedidiah.

I understand that I do 4x3x3x1 to get the answer but I dont understand what that one line of code is saying.

if you add some console.log(), it helps to understand.

console.log(`n = \${n} , return 1`)

console.log(`n = \${n} , \${arr[n - 1]} * multiply(arr, \${n - 1})}`)

You can see :

Result :
4 * 3 * 2 * 1 * 1
= 24

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