Basic JavaScript - Replace Loops using Recursion

I’m new to coding in general. I’ve done the html/css course and understood it somewhat. I’m stuck on the Recursion problem. I know what the answer is because I looked it up. Here’s the instructions and The answer:

//Instructions
Write a recursive function, sum(arr, n) , that returns the sum of the first n elements of an array arr .

//answer

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
}

Why this is the answer I have no clue. I feel like a complete idiot. I surely can’t be that stupid. I was able to graduate college but I guess I am. This makes no sense…any of it. Could someone break this down for me in laymen’s terms. The instructions are in English but to me, it often feels like I’m trying to read Icelandic or something. Very discouraging

Don’t be frustrated too much, please. Recursion is tricky as hell.
Let’s grab this code you provided:

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
}

Lets say if we have array [1, 2, 3, 4] and number 3.

So try to explain step by step, what will happen inside the code, if I call this function with this input:

sum([1, 2, 3, 4], 3)

As soon as you’ll stuck, try to provide question, as specific as it can be, and give it to me.

Also, consider add link to the challenge step to your post, it will be easier for people to help you

My apologies. I’m new here.

Are you saying use the array equation with the number 3? I’m not following. Thanks for helping though

I’ve had to look up solutions to 99% of the JavaScript problems so it isn’t just recursion….so that’s why I’m feeling dumb. I feel like sometimes the solutions are concepts that they haven’t even taught you yet and they seem to pull the solutions out of thin air.

No, I am afraid you misunderstood.
this function

function sum(arr, n)

can take two parameters:
array and number.

So my intention was: to provide specific example of input: of array and number. And see what this function will do with them.

It’s easier sometimes to understand general logic through specific example.
I believe there is fancy word for such approach to learning, but I don’t know it :upside_down_face:

To move through curriculum without proper understanding is not the best approach I guess.
However, recursion is not really the easiest thing. If you have questions about more simple concepts(based on what you said, I guess you have), it may be better to start from there.

As an option:
You can create another thread about some earlier challenge step which confuses you.
We can return to recursion here a little later.

1 Like

whenever you are stuck, ask for help

try to go back to solve those challenges again, if you have doubts on the basics it’s going to be really hard to move forward

I get more of what you mean. What I don’t think I understand is where does n-1 come from? I know it asks for the sum and it shows n-1 in the example but is this just because it’s a formula you plug in? I guess I was hoping for it to ask for the difference as in subtraction. I’m just having a challenging time understanding the directions for some of the problems.

I do feel the html/css section was explained better. The JavaScript section for me has been 0-100 mph

JavaScript is a scripting language, where HTML/CSS is markup
With JavaScript, you have to learn, other than the syntax, the programming logic. Yes, it’s much much much harder, to learn, to apply, to make sense of. That’s why I strongly suggest you go back to the challenges you have solved by looking at the solutions and try again, and if you are stuck, ask for help on the forum instead.

2 Likes

Got it thank you. I may take a break and try other resources outside of freecodecamp. When I don’t know what a lot of the problems are even asking me to do, it’s hard for me to even attempt to solve them.

Once you start copying answers, everything starts to become pretty difficult to understand pretty fast.

Unlike HTML/CSS, Javascript (or any other programming language) is not about remembering syntax - it is about describing a logical process in code.

I get what you mean. My problem is I often don’t understand what the problem is asking me so that I can’t even attempt to answer it on my own. It’s worded in a way that doesn’t make sense to me

that’s a good reason to create a topic on the forum instead of seeing the solutions

1 Like

Once you start looking at the solutions, that will happen more and more, unfortunately.

I agree with @ilenia, the entire purpose of the forum is so that you can ask questions about the challenges.

If there’s a way to delete this thread, feel free. I feel like I wasted everyone’s time in this thread and will do it more appropriately elsewhere.

I don’t think that you wasted anyone’s time. It’s a common misconception that copying Javascript will help you understand how to write your own Javascript.

This thread is useful. You mentioned that you looked up solutions before solving challenges by yourself.
Other members of community were able to tell, that it’s a problem, and they explained why it’s a problem.
This thread contains productive discussion to my opinion.

2 Likes

Hi, please don’t delete this thread. I’m new too, well, I have been trying to learn for about 2 years now.

I have done the HTM/CSS legacy course and I’m currently doing the JavaScript one. I’m also finding it difficult.

You’re not alone in your frustration and confusion. This is helpful. It’s good to see how other people are finding it and knowing that we have shared experiences.

Thanks

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