Tell us what’s happening:
So I understand the basic function of what’s happening in the question/action needed but what I don’t understand is the first portion of sum(arr, n)
. The arr
when it comes to the mathematical function of it if that makes sense.
How do you come up with the answer when you are not seeming to do anything with the array?
I’ve seen it written out to show how it’s supposed to function but that doesn’t explain to me how it works other than showing me an array with a plus whatever number at the end and sometimes with parentheses.
ex: sum([2, 3, 4, 5] + 2) + 1
I don’t get that at all.
This is what I got based on the examples provided in the lesson:
code so far
if (n <= 0) {
return 0;
} else {
return sum(arr, n - 1) + arr[n - 1];
}
My question is this:
What is happening on the else portion of the function?
Some people were saying that the + arr[n-1]
portion signified how many index locations on the first part to add together sum(arr, n-1)
.
I hope this makes sense as to what I found confusing.
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36
Challenge: Basic JavaScript - Replace Loops using Recursion
Link to the challenge: