Can someone help me understand recursion?

Tell us what’s happening:

I don’t know what to do in this challenge i don’t understand whats going on please help!

Your code so far

function sum(arr, n) {
// Only change code below this line

// 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/79.0.3945.88 Safari/537.36.

Challenge: Replace Loops using Recursion

Link to the challenge:

1 Like

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

do you understand this?

let arr = [2,3,4,5,6,7,8]
multiply(arr, 3); // this is equal 2*3*4*5
multiply(arr, 2); // this is equal 2*3*4
// so you can say that line 2 is equal to below:
multiply(arr,2) * arr[3]; // this is equal (2*3*4) * 5

so you can do this various times.
but eventually it needs to stop
so you say that multiply(arr, 0) is equal to 1 (instead of an other function call) (and because 1 is the neutral number in multiplication as q * 1 === q for every number)

is this confusing enough?

1 Like

yes its a bit confusing
first of all i dont know what n means here arr is the array i dont know what n stands for here

is it multiplying arr [1] * n? but here n means a number like 3?

i would like an explanation …

n is a number, and it is the index of the last number in the array that should be multiplied. It is also the second parameter of the function.

function muplitply(arr, n) {...} // see where n is defined?

I made the example with real numbers. In my example n is 4 and n-1 is 3
it says n and n-1 because that relationship is valid for any number

and I sayd a wrong thing before.
multiply(arr, 0) would return arr[0]
you would need to return 1 only if n (second parameter of the function) would be less than 0

1 Like

so the if the base case is if (n <= 0) means n = 0 right? like if n = 4 that means it will select
arr = [1,2,3,4,5]; 5 right?

yes, arr[n] when n is 4 will be 5

but careful with the base case
arr[0] and arr[-1] are pretty different things

1 Like

from the challenge

return sum(arr, n - 1)* arr[n];

what does this mean? total array * by arr[4] ?

this one is a sum function so you will need to use a + to sum the numbers

that is the return statement of the sum(arr, n) function, and it is returning the value from sum(arr, n-1) adding arr[n]

the same as this but with addition:

1 Like

this could help you, try looking at the function in the challenge description with this tool: (remember you need also a function call to see the function executing)

1 Like

This is very confusing i need to take some time off. my brain can’t handle this right now
thank you for the help :slight_smile:

Ill look through all the replies again tomorrow! I might be able to know what’s actually is going on in that code when my brain is clear lol

@ ieahleen Thank you for your very detailed and excellent answers on this post. However, I really don’t understand where n - 1 comes from.

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

Please would you enlighten these dullard grey cells of mine to prevent them merging into a single cell protozoa.

Thank you in advance :slight_smile:

1 Like

It’s also in the lesson
if you read it you will see that it has a function named multiply
and also can see that it does use the same code as above
n - 1
the n is just a placeholder
if you take a closer look at the code you notice the next

i <= n

it is for the equivalence
if you say that multiply(arr,n) is equal to arr[n] multiplied the function call with different parameters, you can’t use again n

1 Like

Hi guys, Thanks for the replies. Sometimes it can be really difficult to ask a question correctly, but after some thought if figured that my confusion arose from the original for loop used to solve the task:

multiply the elements from 0 to n inclusive in an array to create the product of those elements

  function multiply(arr, n) {
    var product = arr[0];
    for (var i = 1; i <= n; i++) {
        product *= arr[i];
    return product;

which caused me to ask where does n - 1 come from in the solution using recursion? Of course the answer is simply:

in the for loop we are counting up to the value of n
in the recursive function we are counting down from the value of n.

Man that hurt my brain!


I’m still trying to understand what’s going on here its been almost a month i still dont understand… not giving up tho


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

sum([1,2,3,4,5], 4);

sum(arr, n - 1) means during the loop it will call the function itself and reduce the number which in this case is n and n is sum([arr], 4);

4 is arr[5]

So it will call it-self till n = 0 which is arr[1]; right?

And will give the sum of 15

1 + 2 = 3;
3+3 = 6;
6+4 = 10;
10 + 5 = 15;
1 Like

yes, that’s correct!

1 Like
 function sum(arr, n) {
  if (n <= 0) {
    return 1;
  } else {
    return sum(arr, n -1) + arr[n];

can you tell me whats + arr[n] in sum(arr, n -1) + arr[n];
so far i understood arr, n-1 to some extent

it is summing the next element in the array to the total…

it is the +5 of here

it is going down like 5 + 4 + 3 + 2 + 1 ?