Please Help - Replace Loops Using Recursion

Hello,

I have been working on this challenge for hours and still do not understand how recursion works. I am trying to just understand the FCC example:

function multiply(arr, n) {
if (n <= 0) { return 1;}
else {return multiply(arr, n - 1) * arr[n - 1];}
}

The following statement has me lost:

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

To better understand the first part, I used console.log, just to isolate this:

multiply(arr, n - 1)

How can multiply(arr, n - 1) equal a value? How does an array and a variable interact to output a value? It has never been stated that n equals the number of elements in the given array.

One last thing. If we are considering zero array elements (because n <= 0), then how can the returned value be 1?

Thank you!

Link to challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/replace-loops-using-recursion

Pls post a link to the exercise.

All the logic are specified inside of the multiply function.

The multiply function does declare a operator * inside the else sentence. It will be executed for every time n > 0. If you declare n=3, then it will executed for the first two elements because n - 1 is equal to 2.

The first time you call multiply(arr, n - 1), n is equal to two. 2 is not less or equal to 0 , hence it call the same function again with the value (arr, n - 1) .

The second time, arr is the same but n is equal to 1, because (2 -1). Again 1 is not less or equal to 0 and the same function is called with n = 0.

The third time, as n is 0, the function finally return 1.

Now that we have a return, we resolve the second step. n is still 1 here and the line multiply(arr, n-1) * arr[n-1] will be 1 (the last returned value) * 5 (the array element in the specified position). This will return 5.

And now, finally we resolve the fist step. n = 2 and the else line will be the same as 5 (again the last returned value) * 10 (again the element in the specified position) and the final result is 50.

Hope it help. Recursive functions can be a little tricky at first.

1 Like

Thank you for your response.

One thing I still don’t understand is when we see:

multiply(arr, n - 1)

How does the function know that n refers to array elements? For example, array[n] refers to array index, because it is a JS feature, but does (arr, n) refer to array elements as a rule?

Isn’t (arr, n) just a statement of an array and a variable? How does the function know that it means “the first n number of elements?”

Link has been posted. Thank you!

“If you declare n=3, then it will executed for the first two elements because n - 1 is equal to 2.”

How does the function know that n means number of elements?

Thank you.

The functions knows what you make it know, you are the coder.

You may want to go back and revise the basics of functions…

The given function never states anywhere that n means elements.

Code gives meaning through code.

So for eg. Find every line that mentions n.
What can we understand about n from those lines?

From looking at every line with n, we can understand that it decrements.

But I don’t understand how it means “number of elements.”

One last thing: How does multiply(arr, n - 1) equal a value? It seems to be a function with a set of parameters, but without any instructions, itself. How can it calculate anything at all?

Thank you, Tyesh.

The function takes an array we want to multiply and a number n which is the number of elements.

We know this because we wrote this function so when we call it the very first time we will give it the array and its length.

To multiply the numbers within the array we have a choice as programmers.

We can use a for loop for eg and a variable set to 1 and go through and just multiply the numbers and store the result one by one till the loop is done.

Or we can use recursion. Which means we can break the array into smaller and smaller arrays and worry about multiplying only the current number (the last one) with whatever the product of the rest of the array is.

So let’s say we start like this
arr is [4,5,6] and n is 3

First Pass:
Is n <= 0? Nope. 
call multiply([4,5,6],2) and wait
   Second Pass:
   Is n <= 0? Nope
   call multiply([4,5,6],1) and wait
     Third Pass:                     
     Is n < = 0? Nope
     call multiply([4,5,6],0) and wait
       Fourth Pass:
       Is n <=- 0? YES! return 1;
     return 1 * 4;
  return 4 * 5;
return 20 * 6;

Thank you for your response.

([4,5,6], 2) = 20
([4,5,6], 1) = 4
([4,5,6], 0) = 1

How is the * operator being applied to the statement multiply(arr, n - 1), when that statement doesn’t have a * operator? And when did we establish that n means number of elements?

the meaning of the variable is all in your brain. You code = you assign meaning.
the computer knows nothing of meaning. It only knows what you type into it.

The * operator is on the else line.
Just read it to the end.