Hello.
Please I have been trying to understand what is happening here but I am finding it difficult to really grasp it before I go to the actual practice work. It is under the replacing loops using recursion but before that, we were told to visualize the following task( multiply the first `n` elements of an array to create the product of those elements, and a for loop was used) and I am finding it difficult to understand what is actually happening in the code below.

``````function multiply(arr, n) {
var product = 1;
for (var i = 0; i < n; i++) {
product *= arr[i];
}
return product;
}
``````

I am finding it a bit difficult to actually understand this so I can move on in that topic. Please I need some help.

1 Like

what is it that you do not understand? if you had to explain this code, where do you get stuck? whatâ€™s your best guess of whatâ€™s going on?

This is what I understand

A function has been declared called multiply and has parameters of an array and a number

I assume n which is the number is used to identify the position of the element in the array

var is the variable for the loop and for the loop to continue i has to be less than the number used to identify the position of the element we need from the array everytime i is increased.

Everytime the code is executed a product is returned which I assume is the product that was declared multiplied by the position of the element that was identified by n.

That product is stored in var product and used for the next loop when i is increased by 1.

This continues until i is no longer less than n

What I donâ€™t understand is this last statement I just made in the previous line and that is if what I have written in the other lines are actually correct because I am not entirely sure because I feel this loop at some point is going to stop and when exactly would that actually happen?

I feel it is going to happen when var i stops been less than n but I am not absolutely sure of the relationship between i,n and arr and even arr[i].

This is how I understand the code and I have not been able to go past this because I feel I have not probably gotten it because if I have gotten it then I used to be able to represent this with a real life example or maybe with numbers

if you do `multiply([1,2,3], 3);`

Itâ€™s going to do

``````// Product * arr[0] = 1
// Product * arr[1] = 2
// Product * arr[2] = 6
``````

Sorry n should be 3 because i < n so it skipped 2.

In this case does it mean arr[i] is equivalent to n or are they different and when is the loop going to end?

1 Like

You wrote in the for loop

The for loops does the multiply, then it increases the i as long as it is less than n.

An example

``````
// does the for loop first time i = 0
// second time i = 1
// last time i = 2
``````

Stops because `i` is not less than `n` anymore

Why is it not less than â€śnâ€ť ?
Because

``````i = 3
n = 3
``````

So it means i and arr[i] are different and what role is n actually playing here? is n not being used to identify the positions of the elements in the array?

Try make a code like this:

``````let arr = [1,2,3,4,5,6,7,8,9,10];

function multiply(arr, n) {
var product = 1;
for (var i = 0; i < n; i++) {
product *= arr[i];
}
return product;
}

console.log(multiply(arr, arr.length));
``````

What number do you get in the console?

n represtents how many times it should do the loop.

Itâ€™s going to do the loop n - 1 because you had `i < n` in the code.

hmmâ€¦
then that means I have been looking at it all wrong.

1 Like

The value that I got in the console was 3628800

1 Like

if you for example make the function like this

``````function multiply(array, b) {
var product = 1;
for (var i = 0; i < b; i++) {
product *= arr[i];
}
return product;
}
``````

or

``````function multiply(Hamburger, Cheese) {
var product = 1;
for (var i = 0; i < Cheese; i++) {
product *= Hamburger[i];
}
return product;
}
``````

Still will do the same when adding this

``````let arr = [1,2,3,4,5,6,7,8,9,10];

function multiply(Hamburger, Cheese) {
var product = 1;
for (var i = 0; i < Cheese; i++) {
product *= Hamburger[i];
}
return product;
}

console.log(multiply(arr, arr.length));
``````

But the names doesnâ€™t explain what it is, thatâ€™s why you have `multiply(arr, n)`.

`arr` stands for array
`n` stands for a number

Is correct, because you do it likes this

``````// product * 1 = 1
// product * 2 = 2
// product * 3 = 6
// product * 4 = 24
// product * 5 = 120
// product * 6 = 720
// product * 7 = 5040
// product * 8 = 40320
// product * 9 = 362880
// product * 10 = 3628800
``````

But is there a scenario whereby n is used to identify the elements in the array like take for example, multiply(arr, n): // whereby arr is [2,3,4,5,6,7,8] and n is 3
so would this mean 2 * 3 * 4

1 Like

Itâ€™s kinda right except itâ€™s

1 * 2 * 3 * 4

because `product = 1;`

If product were 0 the output would be 0.

Try this

``````let arr = [1,2,3,4,5,6,7,8,9,10];

function multiply(arr, n) {
var product = 0;
for (var i = 0; i < n; i++) {
product *= arr[i];
}
return product;
}

console.log(multiply(arr, arr.length));
``````

You were right with `2 * 3 * 4` because 1 doesnâ€™t affect it in the code, but if you had a number less than 1 or more than 1 then you would have to count product in the math.

in this scenario, i used zero based counting which is actually supposed to be 2 * 3 *4 * 5.
Can n be used in that way if we had a function such as:
function multiply(arr, n): // whereby arr is [2,3,4,5,6,7,8] and n is 3?
I am asking this because I saw somewhere whereby it was used that way.

1 Like

To get 2 * 3 * 4 * 5
You would have to give n a value of 4

product canâ€™t be 0 it needs to be 1

Try this if you want n to be 3

``````let arr = [2,3,4,5,6,7,8];

function multiply(arr, n) {
var product = 1;
for (var i = 0; i <= n; i++) {
product *= arr[i];
}
return product;
}

let n = 3
console.log(multiply(arr, n));
``````

Why is it doing 2 * 3 * 4 * 5
even if `n = 3`?

There is a difference with this codes

``````let arr = [2,3,4,5,6,7,8];

function multiply(arr, n) {
var product = 1;
for (var i = 0; i <= n; i++) {
product *= arr[i];
}
return product;
}

let n = 3
console.log(multiply(arr, n));
``````
``````let arr = [2,3,4,5,6,7,8];

function multiply(arr, n) {
var product = 1;
for (var i = 0; i < n; i++) {
product *= arr[i];
}
return product;
}

let n = 3
console.log(multiply(arr, n));
``````

it is doing 2 * 3 * 4 * 5 because it is counting using zero-based indexing

1 Like

Yes arr[0] = the first value in the array.

Also you can see I added `i<=n` instead of `i<n`
This means it is going to do last loop when `i = n`.