I do not understand the JS lesson about replace loops using Recursion

It’s lesson 101, near the end of Basic JS. I do not understand a couple of things.

  1. What does it mean where there are 2 parameters (think they are parameters) in the parentheses of the function? Is that covered in a previous lesson?
  2. In the lesson it mentions "… multiply(arr, n) == multiply(arr, n - 1) * arr[n - 1]" - I don’t know where to start. How do I interpret that?

These last lessons are getting tough for me.

The function has two arguments. This just means that there are exactly two inputs to the function.

This equation
multiply(arr, n) == multiply(arr, n - 1) * arr[n - 1]
means that if you call multiply(arr, n), the result should be exactly the same as if you called multiply(arr, n-1) and multiplied the result of that function call by arr[n-1]. Which is to say, the product of the first n elements in the array is the same thing as the product of the first n-1 elements in the array multiplied by the nth element in the array.

Recursion is all about calling the function from inside itself with a reduced set of inputs until the problem is very easy to solve.

So does [n - 1] stand for the last item in the array? I’m totally confused…

The brackets only make sense when associated with the arr variable.

arr[n-1] stands for the item at index n-1 in the array. The indices go 0, 1, 2, …, n-1, n…

Hi @kernix !

I wouldn’t worry about trying to fully understand recursion on your first try.
Most people struggle with the concept when it is first introduced.

You could look into these additional resources for recursion.

As you progress through the rest of the javascript sections the difficulty will increase.

Just take your time :grinning:

Thanks, I’m just moving on and hopefully things will click before too long…

I wouldn’t skip the lesson outright. Recursion is hard in part because it highlights all of your misunderstandings and holes in your knowledge, and it is important to sort out those problems.

1 Like

I’m not skipping it - I just don’t understand it

I perhaps do not see a difference between skipping a topic and going on past the topic without any understanding?

2 Likes

There is a strong correlation there. :grinning:
The further I move on from what I don’t understand, the more I don’t understand what I left behind me.

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