Basic JavaScript - Replace Loops using Recursion

*Hello there this was difficult for me to grasp , I believe i have understood it for this particular instance so i would like to help fellow students understand it. because it really is confusing in my opinion. please do verify my work in case i am wrong

I have like to think of it as a slinky or a person on a trampoline where an action begins:
i.e something is dropped, and once it reaches the base (base case) , it springs back up to its original position.

lets take this function

function multiply(arr, n) {
      if (n <= 0) {
      return 1;
    }     // base case ends here
// and recursion starts here
 else {
      return multiply(arr, n - 1) * arr[n - 1];
    }
  }

and lets take an example array = [1,2,3,4,5]

and run through all of it by starting with multiply(arr ,5)

when we run through the code the base case is not met so we move to the
else {
return multiply(arr, n - 1) * arr[n - 1];
}

here is where i needed to understand something:
The calculation is not happening and nothing is being returned .
here we call multiply (arr, 5-1(otherwise known as 4)) and we are proceeding to the next level until we reach 0
which makes a stack of unfinished functions waiting to receive permission to continue which is where the base case comes in the base case finally gives a result to the previous function call so that the calculation can be finished which in turns gives a result to the previous level so the calculation can be finished all the way up until the top level where the function resolves.

still very confusing i know , it took me a while to grasp it but this will help :slight_smile: :

it looks like this
return multiply(arr, n =5) * unfinished calc
return multiply(arr, n=4) * unfinished calc
return multiply(arr, n=3) *unfinished calc
return multiply(arr, n=2) *unfinished calc
return multiply(arr, n=1) *unfinished calc
multiply(arr,0) hits the base case and returns 1 as the result of the previous level multiply(arr,1) here calculations start getting finished off because theyre no longer halted by calling the function again

multiply (arr,1) now has a value of 1 so the statement becomes
return 1 * arr [n-1] with arr[n-1] being arr[0] or the previous level in the array
essentially it multiplies this level of n with the previous level of n

and it goes like this here is our array again so u don’t have to scroll with the indexes beneath it to avoid confusion
[1,2,3,4,5]
arr index [0,1,2,3,4]
level of n =1: return 1* arr [0] so returns 11 =1
level of n =2 :return 1
arr [1] so returns 12 =2
level of n =3 :return 2
arr [2] so returns 23 =6
level of n =4 :return 6
arr [3] so returns 64 =24
level of n =5 :return 24
arr [4] so returns 24*5 = 120;

and there you have it i hope this helps because it was hard to understand it
if it doesn’t i synthesised the explanation from here maybe he can help you better :
Recursion for Beginners: A Beginner's Guide to Recursion - YouTube;

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