3+ days stuck on trying to learn Recurrsion and finish problem

I need help understanding this function works

Basic JavaScript: Replace Loops using Recursion

Recursion is the concept that a function can be expressed in terms of itself. To help understand this, start by thinking about the following task: multiply the first n elements of an array to create the product of those elements. Using a for loop, you could do this:

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

However, notice that multiply(arr, n) == multiply(arr, n - 1) * arr[n - 1] . That means you can rewrite multiply in terms of itself and never need to use a loop.

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

The recursive version of multiply breaks down like this. In the base case, where n <= 0 , it returns 1. For larger values of n , it calls itself, but with n - 1 . That function call is evaluated in the same way, calling multiply again until n <= 0 . At this point, all the functions can return and the original multiply returns the answer.

Note: Recursive functions must have a base case when they return without calling the function again (in this example, when n <= 0 ), otherwise they can never finish executing.

Write a recursive function, sum(arr, n) , that returns the sum of the first n elements of an array arr .

1 Like

what in particular? you have just pasted here the whole challenge description

I have understood the very first function multiply example ( the for loop ) but i dont understand the recursive function that replaces it i have spend days trying to understand how the recursive function works and gets the results it gets but i dont get it

if you get this, then it is a great step in the right direction

so, multiply(arr, n) should have as output the result of arr[0] * arr[1] * ... * arr[n-2] * arr[n-1]
instead multiply(arr, n-1) should have as output the result of arr[0] * arr[1] * ... * arr[n-2]
if you multiply arr[n-1] to the value of multiply(arr, n-1) you get the value of multiply(arr, n

to get the value of multiply(arr, n-1) you can use the value of multiply(arr, n-2) multiplied by arr[n-2]

you can get the value of multiply(arr, 2) from the value of multiply(arr, 1) multiplied by arr[1]

the other thing you need is a base case, which you need in this case:
the value of multpiply(arr, 1) comes from the value of multiply(arr, 0) multiplied by arr[0]

the value of multiply(arr, 0) needs to be the base case, as there is not a value like arr[-1], so the value of multiply(arr, 0) is 1

let’s say you have an array like const arr = [2,4,5,4,5,6,7,8] and the function call is multiply(arr, 3)
so now we have that: multiply(arr, 3) = multiply(arr, 2) * 5 = (multiply(arr, 1) * 4) * 5 = ((multiply(arr, 0) * 2) * 4) *5 = (((1) * 2) * 4) *5

it is the same thing that happens in recursion, only once the last function call has a value it is possible to start resolving all the functions that have stayed waiting till now, so: (((1) * 2) * 4) *5 = ((2) * 4) * 5 = (8) * 5 = 40

just the first few sentences of that i don’t understand .
what does this mean …so, multiply(arr, n) should have as output the result of arr[0] * arr[1] * ... * arr[n-2] * arr[n-1]??

by definition, the function takes as parameters an array and a number, and the output is the product of the first n elements in the array
the n-th element is at index n-1, so the output is the product of all the elements from index 0 to index n-1

maybe I’m not sure how to think of this function and its parameters its confusing…okay 1 sec trying to understand what you just wrote above

how is the output the product of the fist n elements in array?

what do you mean by n elements of arr

i understand arr is an array and n is a parameter for a number we give it

sorry for being so slow and confused

if n is 3, that means that the output should be the product of the first 3 elements in the array, so the product of elements from index 0 to index 2
whatever n, the output is the product of the first n elements in the array

one part i was probably thinking wrong is the n-1 i was thinking if i pass into the function multiply([1,23],2) then the ‘n’ meaning 2 would be subrtracted 1 from it…

okay one moment trying to understand…lol

im sorry I’am still confused and frustrated i dont understand this However, notice that multiply(arr, n) == multiply(arr, n - 1) * arr[n - 1] at all still

i dont know how the first function equal the secound function mutiplied by the array index n-1 ?

I’m going to move on for today because of the amount of hours of spent or days rather going no where with this its very frustrating and i need to learn something so i can try to grow and change thank you for your help much appreciated

this was my explanation, what’s unclear there so I know what part needs to be explained differently?