Help - Basic JavaScript: Replace Loops using Recursion


I am learning the basic JS course, and I am stuck in this part.
This is the course page:

  1. Can someone please explain to me, what does this means?
    "multiply(arr, n) == multiply(arr, n - 1) * arr[n - 1] "

  2. I got the code right for the challenge, but I still don’t understand:
    this is my code:

function sum(arr, n) {
// Only change code below this line
if (n<=0){
return n;
return sum(arr,n-1)+ arr[n-1];
// Only change code above this line


How do you calculate so sum([2, 3, 4], 1) equal 2 ?

I am super frustrated . Please help. Thank you so much!

1 Like

Don’t feel bad, recursion is really tough. IMO you’re good for now. I’d just keep moving and learn as much about other things as possible.

multiply(arr, n) == multiply(arr, n - 1) * arr[n - 1]

Basically this means you can sub out the part on the right for the part on the left. As to why that is?

Smarter people than me can explain it, but I’ll tell you what I do when I have a difficult loop or equation–make a chart. Make a chart that shows the value of arr, n, n-1 arr[n-1], etc., and then show how it changes as n changes. Do that a bunch of times and that might help.

If you can see it, working it out step by step, maybe it will sink in a bit.

Check out this article:

Hi, thank you for the explanation. The tutorial link was really helpful!

1 Like