Replace Loops using Recursion 292

If am trying to complete this challenge and does JavaScript get easier the more you use it, I’ve managed to do part of the challenge and I’m struggling with the other half can I please have some help.

Your code so far


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

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36.

Challenge: Replace Loops using Recursion

Link to the challenge:

Hello there,

Looking at the example, you need to remember: Recursion is not defining the same function within itself. It is just calling itself within its scope.

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

Hope this helps

1 Like