I don't understand the n - 1

Tell us what’s happening:
Describe your issue in detail here.
So, as far as reading and understanding the code I’m stuck. I don’t understand where they got the n - 1. Why does multiply(arr, n) == multiply(arr, n - 1) * arr[n - 1]? and why can I use that in both an addition problem and a multiplication problem? That part isn’t explained and I don’t understand where they got it from.

  **Your code so far**

function sum(arr, n) {
// Only change code below this line
if(n <= 0) {
  return 0;
} else {
  return sum(arr, n - 1) + arr[n - 1];
}
}
// 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/96.0.4664.45 Safari/537.36

Challenge: Replace Loops using Recursion

Link to the challenge:

Recursion is tough! Not so many people understand it properly. I couldn’t even think of using it before watching this video Recursion in programming. So I suggest you watching it)

1 Like