# What does -1 mean in this code?

Cuéntanos qué está pasando:
I don’t understand which function fulfills the -1 in the first lesson on recursion

this is the problem that the code solves.

multiply the first n elements of an array to create the product of those elements

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

``````
``````  **Información de tu navegador:**
``````

El agente de usuario es: `Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36`

Desafío: Reemplaza bucles usando recursión

Enlaza al desafío:

Imagine you have an array named `arr`, and a number named `n`. You can use `n` and some arithmetic to access values of the array.

``````const arr = ['a', 'b', 'c'];
const n = 2;

arr[n] // same as arr, evaluates to 'c'
arr[n - 1] // same as arr, evaluates to 'b'
arr[n - 2] // same as arr, evaluates to 'a'
``````

1 Like
``````  let say you called multiply([9,5,3],2)
//multiply first 2 elements => 9*5 =45

in first run of multiply
n!==0
so this will execute else statement
i.e. return multiply([9,5,3],1) * arr //calls multiply again and wait for it to return
==================
A            * 5
|
V
in second run of multiply
n!==0
so this will execute else statement
i.e. return multiply([9,5,3],0) * arr //calls multiply again and wait for it to return
===================
B           * 9
|
V
this time n==0
so this will return 1

now final answer is A*5
A = B*9
and B=1     -> A=9
so the final return from multiply function will be 45 which is the answer

``````
1 Like

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