Replacing loops with recursion

Cant seem to pass recursion test cant get second call to equal 2 can anyone help ?

Your code so far


function sum(arr, n) {
// Only change code below this line
if (n<=1){
return 0;
} else{
return sum (arr, n - 1) + arr[n];
}
// Only change code above this line
}
console.log(sum([2, 3, 4, 5], 3))

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3233.0 Safari/537.36.

Challenge: Replace Loops using Recursion

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

it’s a small issue, but important: the variable n indicates how many numbers you need to sum, not the index, so if you need to sum the first 3 (n) numbers you need to sum up to index 2 (n-1)
Instead you are summing also the element at index n, but are never summing the element at index 0 or 1 (your base case use different logic than the rest)

Hi,

look at the provided example with the multiplication:

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

Now let’s imagine we call this function with multiply([2,3,4], 2). So it should give us 6, because the “user story” said: multiply the first n elements of the array and return the product.

When you put that into the function above, the execution happens as follows:

  function multiply([2,3,4], 2) {

// n is not 0, it is 2. so we continue to the else block.
    if (n <= 0) {
      return 1;
    } else {
      return multiply([2,3,4], 2 - 1 ) // <-- new (recursive) call of the function!! second argument of the call is 2-1 = 1 now. 

// Second time the function runs.
        {
// n is still not 0, it is currently 1, so we go to the else
         if (n <= 0) {
           return 1;
         } else {
           return multiply([2,3,4], 1 - 1)  // <- third time the function is called.

//this third time n = 0, so the function does not go to else, but returns only 1
// effectively this is like "multiply by 1"
                    {
                        if (n <= 0) {
                          return 1;
                        } else {
                          return multiply(arr, n - 1) * arr[n - 1];
                        }
                      }

// Multiplying from the second call where n = 1
// so this is arr[2-1] = arr[1] = 3
// effectively it says "multiply by 3"
// take the above third call of the function (which closes, because n=0),
// we now have 1 * 3
* arr[n - 1];
         }
       }

// Multiplying from the very first call where n was 2 (you remember?)
// so this is arr[2-2] = arr[0] = 2
// effectively this section evaluates as "multiply by 2"
// connect it to the first + second close of the call, we now have
// 1 * 3 * 2 = 6
* arr[n - 1];
    }
  }

Now think the same way for addition. I found it helpful to actually take a piece of paper and write down the different function calls and returns.

Have fun!
Sebastian.