Tell us what’s happening:
I just can’t understand this part of the lesson of "Replace loops using recursion."
However, notice that multiply(arr, n) == multiply(arr, n - 1) * arr[n]
. That means you can rewrite multiply
in terms of itself and never need to use a loop.
especially this part.
multiply(arr, n) == multiply(arr, n - 1) * arr[n]
can anyone please tell what’s happening in this code? why they are equal?
Your code so far
function sum(arr, n) {
// Only change code below this line
let product = arr[0];
for (let i = 1; i <= n; i++) {
product += arr[i]
}
return product
// Only change code above this line
}
console.log(sum([2,3,4], 1))
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 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-recursionstrong text