Tell us what’s happening:
Describe your issue in detail here.
Hi there. I am at a challenge on the JavaScript course and I am a noob when it comes to coding. I followed the example shown in the explanation and I managed to come up with a solution, but I don’t understand it at all. Can someone break it down for me and explain it in basic terms, just so dumb people like me (lol) can understand it? Thank you in advance. 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
}
console.log(sum([1], 0));
Can you describe to me which parts you understand or how you got to your solution. Can you tell me what parts are confusing? You were able to solve the challenge, so I think you understand more than you think you do.
As ArielLeslie says, it would be helpful if you are more specific about what exactly what is confusing.
I will add that recursion is a confusing topic and takes a long time to master. For many people it can be incomprehensible in the beginning. So, relax, you’re in good company. In addition to asking a more specific question, I might suggest checking out youtube videos - I think that some aspects of recursion are better understood visually.
Hi there, Kevin and Ariel, thanks for the reply.
To be honest, I would not have been able to get this done without looking at the example given in the explanation and the hint.
Example in the exaplantion:
function multiply(arr, n) {
if (n <= 0) {
return 1;
} else {
return multiply(arr, n - 1) * arr[n - 1];
}
Hint:
Hint 1:
When n <= 0sum(arr, n) returns 0 .
Hint 2:
When n is larger than 0 sum(arr, n) returns sum(arr, n - 1) + arr[n - 1]
So it’s kind of hard to explain what I do not understand as I simply don’t understand it, I hope that made sense. But I will try to explain. Here is the challenge: Write a recursive function, sum(arr, n) , that returns the sum of the first n elements of an array arr .
For the first part of the if statement, I don’t get why I have to return 0 at all and why that needs to be the case only if the condition (n <= 0) is true. For the second part after ‘else’ is the one that confuses me the most, “return sum(arr, n - 1) + arr[n - 1];” Why do I have to call the function “sum(arr, n)” as sum(arr, n - 1) inside the same function, and why do I have to add that to “arr[n - 1]”, what even is “arr[n-1]”?
So, as you can see, a complete noob when it comes to coding, I hope I didn’t confuse you even more.
And again, it is very common for people to have trouble with recursion. It is a weird topic. And I’ve also known more than a few successful developers that never really wrapped their head around it. It’s useful to learn, but don’t beat yourself up over it.
And maybe give it some time - come back to later. Recursion is mainly useful with very specific types of data structures and problems. In 2.5 years as a professional dev, I’ve only written one recursive function. It’s also useful for showing off in interviews.
I would also not call recursion “basic JavaScript”. It’s at best and intermediate topic. And to some extent not a JS topic but is a general algorithm question - it can be implemented in almost any language.
So, learn it, for sure. But don’t worry if you don’t understand it completely right now.