Basic JavaScript - Replace Loops using Recursion

Newbie here: I need someone to see if Im understanding this recursion challenge correctly

I have been stuck on this challenge for almost a week trying to understand it. I could not wrap my head on >>return sum(arr, n - 1) + arr[n - 1] part of the code and how this code was being executed. After looking into many resources I think I finally understand it. Hopefully someone can review my train of thought and see if I’m on the right track!I Apologize in advanced if I didn’t structure this post correctly or if its a bit confusing! :sweat_smile:

//Im going to be breaking it down on how I think this code is being executed

// This what we are calling are function with: sum([2,3,4,5,6,7], 5)

function sum(arr, n) {
// Only change code below this line
if (n <= 0) {
return 0
} else {
return sum(arr, n - 1) + arr[n - 1]
}

// Since 5 is not less or equal to 0 we will move on to are Else statement with 5-1 which = 4
// return sum([2,3,4,5,6,7], 5) + arr[5]
// This when the recursion starts and the function starts calling itself

// return sum([2,3,4,5,6,7], 4) + arr[4] = 6 (since its the 4th element in are array)
// n does not = 0 so it will call the function again with 3
// return sum([2,3,4,5,6,7], 3) + arr[3] = 5 (since its the 3rd element in are array)
// n does not = 0 so it will call the function again with 2
// return sum([2,3,4,5,6,7], 2) + arr[2] = 4 (since its the 2nd element in are array)
// n does not = 0 so it will call the function again with 1
// return sum([2,3,4,5,6,7], 1) + arr[1] = 3 (since its the 1st element in are array)
// n is now is equivalent to and will return the 0
// return sum([2,3,4,5,6,7], 0) + arr[0] = 2 (since in are array indexing starts at 0 )

// After we get the value of 0 we add all the elements together to get are output
// 6 + 5 + 4 + 3 + 2 = 20
// Only change code above this line
}

console.log(sum([2,3,4,5,6,7], 5))
// This should output 20

Your browser information:

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

Challenge Information:

Basic JavaScript - Replace Loops using Recursion

There’s that joke about understanding recursion, which I like to interpret as indicating there might be few points when somebody has the Aha! moment regarding recursion. It is confusing.

For sum([2,3,4,5,6,7], 5):

// Recursive calls, step-by-step getting closer to the base case
sum([2,3,4,5,6,7], 5) -> sum([2,3,4,5,6,7], 4) + arr[4]
// value of sum([2,3,4,5,6,7], 4) is not (yet) known, it's calculated in recursive call
sum([2,3,4,5,6,7], 4) -> sum([2,3,4,5,6,7], 3) + arr[3]
// value of sum([2,3,4,5,6,7], 3) is not (yet) known, it's calculated in recursive call
sum([2,3,4,5,6,7], 3) -> sum([2,3,4,5,6,7], 2) + arr[2]
// value of sum([2,3,4,5,6,7], 2) is not (yet) known, it's calculated in recursive call
sum([2,3,4,5,6,7], 2) -> sum([2,3,4,5,6,7], 1) + arr[1]
// value of sum([2,3,4,5,6,7], 1) is not (yet) known, it's calculated in recursive call
sum([2,3,4,5,6,7], 1) -> sum([2,3,4,5,6,7], 0) + arr[0]

// Base case is reached
sum([2,3,4,5,6,7], 0) -> 0

// Going back from the recursive calls
sum([2,3,4,5,6,7], 1) -> sum([2,3,4,5,6,7], 0) + arr[0] ->  0 + arr[0] -> 0 + 2 -> 2
sum([2,3,4,5,6,7], 2) -> sum([2,3,4,5,6,7], 1) + arr[1] ->  2 + arr[1] -> 2 + 3 -> 5
sum([2,3,4,5,6,7], 3) -> sum([2,3,4,5,6,7], 2) + arr[2] ->  5 + arr[2] -> 5 + 4 -> 9
sum([2,3,4,5,6,7], 4) -> sum([2,3,4,5,6,7], 3) + arr[3] ->  9 + arr[3] -> 9 + 5 -> 14
sum([2,3,4,5,6,7], 5) -> sum([2,3,4,5,6,7], 4) + arr[4] -> 14 + arr[4] -> 14 + 6 -> 20

As you say there’s many resources about recursion, I really like the lecture from Eric Grimson from the MIT 6.00.1x. Code examples uses Python, but they shouldn’t be hard to follow when you know JavaScript syntax.

There’s also helpful tool, that allows for visualizing how code is exacly executed:
https://pythontutor.com/javascript.html#mode=edit

3 Likes

Brother! Thank you for the time you took and the great explanation on recursion. I see that I was almost there. The function needs to reach the base case before we start indexing. Now it is starting to make sense. I will be looking at the video to get a better understanding and also the tool is very helpful! Thanks again! Really appreciate it :fist_right: :boom::fist_left:

1 Like