# Basic JavaScript - Replace Loops using Recursion

Tell us what’s happening:
Describe your issue in detail here.
I got the code right but i do not understand the code
How does this work?
`return sum(arr, n-1) + arr[n-1]`

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

User Agent is: `Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36`

Challenge: Basic JavaScript - Replace Loops using Recursion

1 Like

We have blurred this solution with `[spoiler][/spoiler]` tags so that users who have not completed this challenge can read the discussion in this thread without giving away the solution.

Whoever wrote the solution should know - ask him.

1 Like

I wrote the solution based on the example given.

So it sounds like it is the larger issue of not understanding recursion.

That is very common. Recursion is weird. There have been many, many, many discussions about it in the forum, many on this exact questions, some with detailed explanations, some even by me. I would suggest search those first.

But like I said, it is a tricky subject. Don’t be discouraged if it takes a while to wrap your head around it.

1 Like

Hi @andazi and welcome

You can use this tool to visualizing your code running Python tutor (it works with other languages too)

Remember call your function too, for example `sum([2, 2, 2, 2], 2)`.

It is always helpful read different examples and explanations. The best two pages as reference are MDN Web Docs and this one Recursion and stack

A recursive function is like a family tree, you call the function and that function call another one with different arguments and so on (going down). After, the function start to run from the last one (with the last possible arguments) to the started point (going up in the tree). Every returning value it will be use for the parent function as keep going up.

I hope that helps

1 Like

Hello @andazi.

In order to understand this lesson, it is useful to analyze the information provided in more detail:

• The name of the lesson is “Replace Loops using Recursion”
This implies that they are very similar

• There are 2 examples of the function `multiply`:

• `multiply` using a for loop (left)
• `multiply` using recursion (right)

• The lesson states that:

# multiply (for loop,left) vs multiply (recursion, right):

what are the differences between the 2 versions of `multiply`?

Note:
I have made a video explaining (step by step) this lesson freeCodecamp: "Replace Loops Using Recursion", Solution. - YouTube .

1 Like

I’m sure this must have also been discussed many times, but I find it weird that the tutorial assumes prior knowledge of some math concepts (not even sure what exactly):

However, notice that `multiply(arr, n) == multiply(arr, n - 1) * arr[n - 1]` . That means you can rewrite `multiply` in terms of itself and never need to use a loop.

This is not at all clear and further explanation should definitely be provided on how this transformation occurs.

The prior knowledge of some math concepts would be multiplication.

That line says that multiplying `n` numbers together is the same thing as multiplying `n-1` numbers together and then multiplying that result by the last number.

I don’t think it’s a stretch to say that for the vast majority of learners familiarity with multiplication in itself does not lead to instantly comprehending what you just described. I may be wrong though.

Right. Recursion is hard. But my point is that isn’t hard because of some magic missing math concept. It’s hard because people aren’t used to thinking about a problem as a formal process that has a relationship to sub-problems.

The only math concept needed to understand multiplication with recursion is multiplication itself. The math isn’t the hard part. Its the recursion that’s the hard part.

Gotcha. I guess it’s just that this topic feels far more difficult compared to the learning curve of the previous ones in that tutorial series. And then there’s that very brief explanation I quoted, which I feel doesn’t help very much either.

I think I now understand why some of the other popular tutorials out there don’t even touch this topic alongside other basics.

Yeah, recursion is notoriously hard. Its a weird concept to wrap your head around, and any misunderstandings you might have in function calls, return values, and function scope amplify this difficulty. Unfortunately though, misunderstanding is part of the learning process.

I haven’t found a single description that works for everyone. Best approach I’ve found so far is to have a basic description and a place for people to ask questions as they dig deeper and play around with the idea.

1 Like

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