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.
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
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?
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
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.
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));
The visible difference I see between these two is the greater than and greater than or equal sign on the i < n and i <= n but how exactly is this affecting the value output on the console because one of them gives 120 and the other 24?