Please help better clarify this loop code

Multiply this 24 * 5

i < n never does 24 * 5
but i <= n does 24 * 5

Check out this link to learn more Loops and iteration - JavaScript | MDN

Oh I see. I would check out the linked source to get even more understanding.
Thanks a lot for taking the time to clarify this and answer my questions. It has helped me a lot.
I really appreciate it.

1 Like

They have this example with a for loop

for (let step = 0; step < 5; step++) {
  // Runs 5 times, with values of step 0 through 4.
  console.log('Walking east one step');
}

But if you do <= instead of <
step is going to end up as 5 instead of 4.

for (let step = 0; step <= 5; step++) {
  // Runs 6 times, with values of step 0 through 5.
  console.log('Walking east one step');
}
1 Like

the function multiply the first n elements in the array, that’s what’s n is for

2 Likes

Hi there, I will try to explain the better way posible what is happening there.

1.- “n” is not used to identify anything on the function. “n” is used to determine the number of elements in the array will be multiply.

2.- When you use:
for (var i = 0; i<n; i++){
arr[i];
}

You use “i” to identify the element on the array and “n” to determine how many times you will iterate the array with the increment “i++” (that is what for is mean to)

You can read on the task:
multiply the first n elements of an array to create the product of those elements

So if you understand that sentence, that is what it means “n” on the function.

multiply ([1,2,3,4,5], 3) will return the product of 1 2 3 = 6
multiply ([1,2,3,4,5], 4) will return the product of 1 2 3 4 = 24

If you change i<n to i<=n, that will give you more than ‘n’ loops

You can do:

for (var i = 1; i<=n; i++)

This way ‘n’ will “identify” the position in the array. If you need to know it for reasons.

1 Like

Alright, I now understand it even better. Thanks for the help

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