Basic JavaScript - Replace Loops using Recursion

Sorry if this is kind of a “dumb” question, but the wording is throwing me off.

“multiply the first n elements of an array to create the product of those elements”. I’m trying to translate that sentence another way, but can’t seem to know what “the first n” means.
Your code so far

function sum(arr, n) {
// Only change code below this line
console.log(`the array is [${arr}]`);
console.log(`the number is ${n}`);
// Only change code above this line
}
  **Your browser information:**

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

Challenge: Basic JavaScript - Replace Loops using Recursion

Link to the challenge:

In this case n is basically a placeholder for a possible number. So if i gave you the number 3 you would multiply the first 3 elements of an array, but it could be any number for n. So if you check the test they have function calls like sum([1], 0) or sum([2, 3, 4, 5], 3) where the number at the end represents the amount of numbers you should sum together from the arrays.

Good luck, recursion is a fun one to learn!

Thanks for the explanation.

Here though,

“However, notice that multiply(arr, n) == multiply(arr, n - 1) * arr[n - 1]” I don’t know WHAT to notice lol.

What does that mean? How is multiply(arr, n) the same as multiply(arr, n - 1) * arr[n - 1]?

This is the product of the first n elements

This is the product of the first n-1 elements multiplied by the nth element.

I don’t get it :rofl: I apologise

How are those two calculations the same though

If I have 5 numbers [2, 3, 1, 3, 4], the product of the first 4 numbers is 18.

Using this formula instead, if I multiply the product of the first 3 numbers, 6, by the 4th number, 3, I get 18, which is the same thing as ‘the product of the first 4 numbers’.

Sorry, where did you get 18?

I used multiplication. 2*3*1*3=?????

The word product means ‘the result of doing multiplication’.

1 Like

You mean 2 * 3 * 1 * 3 right?

Yes, 2*3*1*3

Oh I didn’t know that meant multiply every number. i thought we were multiplying it by the number from the input.

yeah sorry i typed that before I saw you edited it lol

From the instructions:

To help understand this, start by thinking about the following task: multiply the first n elements of an array to create the product of those elements.


Yes, I edited to fix the formatting

I didn’t know what multiply the first n elements meant. When I log N to the console I got 0 1 3. I know that sentence was in the instructions, I just did not understand.

The entire sentence matters. (In general with technical explanations the entire sentence matters and skipping parts of a sentence can change the meaning) Do you know what ‘the product of the elements’ means?

Not understanding the second formula. “Using this formula instead, if I multiply the product of the first 3 numbers, 6, by the 4th number, 3, I get 18, which is the same thing as ‘the product of the first 4 numbers’”. I’m sorry lol. Why would we even need to do that? I’m confused

Did this part make sense?

Yes, I thought it meant take N and multiply N by each number

That’s not what ‘the product of those elements’ means. That would be ‘multiply each of those elements by n’.