# Replace Loops using Recursion - I think I get it? Halp

Tell us what’s happening:
Okay, after reading several forums and writing this out by hand several times, I think I know what’s happening and want to check my knowledge.

The first thing that I had to understand was that `n = the number of values in an array`. Did we learn this previously and it just got lost in my brain? I learned this through a forum and not from the wording in the lesson.

The second thing I had to understand was that in this particular type of function, when you say sum(arr, n) the n is telling you how many numbers in the array to add together. Not just add the array plus the n value. Is this a quality within the Math functions?

Third, n acts differently in the two parts of the function (this is where I think I might be wrong, and it just works coincidentally). In the first part ` return sum(arr, n-1)` n-1 returns a number and not an index. Additionally, the number passed into the function is taken at face value, and NOT as `n = the number of values in the array`. In the second part `arr[n-1]` returns an index. So if we take one of the test cases `sum([2, 3, 4], 1)` we can think of it as

`sum([2,3,4], 0) ` (because n=1 and not the 3 values in the array, therefore 1-1=0) which tells us sum ZERO of the numbers together. So we have zero at the beginning of the function. For the second half we have `arr[0]`. Because the second part of the function is an index, it’s referring to the 0th place in the array, or 2.

If I take an example not in the tests, let’s say sum([4,5,10],3) I would come up with…

`([4,5,10], 2) + arr[2] = 19. ` Correct?

What happens if the n passed through is more than the number of items in the array? Say… ([2,4,6], 5) would it be as simple as

([2,4,6], 4) + arr[4] = 12? Since arr[4] doesn’t exist and you can’t add more than three numbers for the first half, you just add as many as possible? (Just tried this with console.log and the result was NaN.) Help me understand plz!

``````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
}
console.log(sum([1],0))
console.log(sum([2,3,4],1))

``````

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

Challenge: Basic JavaScript - Replace Loops using Recursion

For this particular function, `n` equals the first `n` numbers in the array that you want to sum together. The array could have 100 numbers in it, but if `n` equals 2 then you only add the first two numbers in the array together. And it doesn’t have to be called `n`. You can use any name you want to represent this value.

OK, I think you are starting to get what `n` represents here. This has nothing to do with qualities in math functions. The `n` just represents an argument passed into the function. In this case it is how many elements from the beginning of the array you want to add together. The function definition could have been “add the last `n` elements in the array together” and then `n` would represent the number of elements to add together starting from the end of the array instead.

This is almost correct, but you left out one important thing, the function name:

``````sum([4,5,10],3) // is the same as

sum([4,5,10],2) + 10 // I replaced arr[2] with its actual value
``````

So now keep going, what is the next line in the above? What can we replace `sum([4,5,10],2)` with? You need to keep replacing until you no longer have a function call (i.e. you hit the base case).

Thanks for the reply! And good to know that it’s not just n and you can use any value. For the comment you made about “the last n elements” would you use a decrementer? Like `i--` in your for loop? i’ll have to think about how to put that into a recursion statement tomorrow when my brain is no longer melting

``````sum ([4,5,10], 3) // is the same as

sum([4,5,10],2) + 10 // is the same as

sum([4,5,10],1) + 5 + 10 // is the same as

sum([4,5,10],0) + 4 + 5 + 10
``````

Is that right? I don’t get how this is simpler than using a for loop

Perfect! Hopefully it is clearer now how you are using recursion to build up the total.

It’s not. You would not use recursion for this specific scenario in the real world (unless you were using a language that doesn’t support looping). They are just trying to use a fairly simple example because the concept of recursion is confusing enough as it is

1 Like

Thank you!! Feeling a lot more confident now

Ah good to know. Thank you!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.