How well do I have to understand recursive functions?

OK, so I realize this may be an unanswerable question, but How well do I have to understand recursive functions at the point of this challenge in the beginner course?

I ask because it seems like this sort of thing should be a course all on its own. Like it’s a more advanced computer science than a beginner course should include. I guess it all depends on a person’s long term goals. But as I approach the end of the beginner course, I’m wondering if I should understand this better before I move on to the next course?

After not having any luck I took a look at the hints, and there were none! That seemed kind of odd.

So I looked at the solutions. The first one makes sense, in that it is just like the example in the lesson but instead of putting new numbers at the end of the string it puts them at the beginning. At this point, is it enough to understand that if the example in the lesson counted up, then this is how to do the opposite?

When I look at solution #2, I don’t think the course covered the splice method did it? Also, having investigated, this seems like overkill when unshift is a thing.

With solution #4, there is ... which I really don’t think was covered in the course.

And as a side question, is there some document that shows an overview of what was learned in the course? Like a reference? Or should I have been making my own as I go along?

Thanks!

1 Like

I’ll add that at this point my general goal is to be able to use js on WordPress websites that I manage, and specifically adapting code so I can replace plugins. For example I recently was able to use some js I found to replace a plugin that did progress counters.

1 Like

I would say to not worry about them too much. It is a difficult concept to grasp and it can take a while. And to be honest, unless you are working with very specific data types, they don’t really come up much. But they are good for your “coder brain”, they do come up on interviews, and when you need them they are life savers.

So, do what you can, and maybe come back to it from time to time and shore up your understanding until they “click”.

I don’t think the course covered the splice method did it? Also, having investigated, this seems like overkill when unshift is a thing.

I don’t know if it was explicitly covered, but it is a good idea to look on MDN and be at least familiar with the different prototype methods.

With solution #4, there is ... which I really don’t think was covered in the course.

It (the spread operator) was definitely covered in the ES6 section. But sometimes this stuff takes a few passes to sink in.

And as a side question, is there some document that shows an overview of what was learned in the course? Like a reference? Or should I have been making my own as I go along?

Not to my knowledge, but I like that idea - an outline with links and/or a glossary with links.

4 Likes

Recursion is brain-bending and I rarely use it in production code.

That said…

What I do like about learning recursion is that it tends to highlight common weaknesses learners have, to include:

  • function definitions vs function calls

  • return values

  • variable scope

To put it another way, a big part of why the recursion lessons are hard is because they rely upon key knowledge that learners often misunderstand. These misunderstandings need to be cleared up if the learner is to be a successful programmer.

4 Likes

@kevinSmith Thanks! That’s all really good to hear.

So this challenge is in the Basic JavaScript course. It looks like the ES6 course comes after? I’m not complaining. But in general, should I expect that sometimes solutions are given that may contain things not covered in the curriculum so far?

2 Likes

Yes. People come to freeCodeCamp with a variety of knowledge, and sometimes people use earlier lessons to practice newer syntax.

1 Like

That’s what I thought! I really appreciate your insights.

To be clear though, the couple of times I’ve used recursion in production code, it was the clearest and most straightforward way to represent the logic involved in the task at hand.

2 Likes

That’s a fair point. Realize that the suggested solutions are just things that people submitted so they may contain ideas not yet covered. They are just solutions that work, not necessarily “solutions that work only by using what we’ve learned so far”.

2 Likes

Yeah, I don’t know if I’d say that recursion is rarely used, I’d say that it’s rarely used for most people. If you’re working with a lot of trees and graphs, then it you use it a lot. The two times (in 3 years as a professional dev) that I’ve used a recursive function, they were both for traversing trees. I could have done them iteratively, but the recursive solution was muuuuuuuuch cleaner and easier to read.

2 Likes

Only if you understand how recursion works in the original example in the challenge. The point of this challenge is not to hack a working example but rather to understand how recursion works.

If I recall correctly, recursion was introduced in my first term of CS101, so this is actually a pretty standard idea to teach early on. Granted, if you are just doing front end development then you probably won’t use recursion much, if at all, but understanding recursion does have some benefits which help you understand how functions and scope work in general. Also, recursion might come up in a technical interview (even if the job you are applying for probably won’t use it) so it doesn’t hurt to understand the basics.

Could you click the Reset All Code button and complete this challenge with no extra help? I would at least try to get to that.

2 Likes

I really need to understand things before I can move on. So when I encountered recursion I spent an afternoon watching YouTube videos of people explaining it. I just searched “recursion JavaScript” and found loads of them. I think I’ve got the idea now.

3 Likes

I guess what I’m mentally stuck on is that in the case of the last two lessons of the beginner course, a recursive function seems like the least straightforward way to solve the task of counting up and down. :laughing: But I totally get that you have to learn the concepts somehow. Just seems like an overly difficult thing to put in a course that spends dozens of lessons just on simple operators.

I used to be like that too, but then I realized that courses aren’t always setup so that each concept is essential to understand before you move on to the next one. And that sometimes it’s dangerous to get bogged down in concepts that are actually more advanced that one’s current level of understanding.

I was just trying to figure out if recursive functions was one of those things, and I think I have my answer.

Thanks so much for everyone who took the time to contribute! This seems like a wonderful community and I’m glad I’ve found it!

2 Likes

I guess what I’m mentally stuck on is that in the case of the last two lessons of the beginner course, a recursive function seems like the least straightforward way to solve the task of counting up and down.

Absolutely. 100%. It is a dumb way to solve the problem. We agree on that.

But recursion is a difficult topic and this is a relatively easy recursion problem. While the recursion is still awkward, at least the task is simple and straightforward.

With regards to recursion in general, there is always an iterative solution - you never need recursion. But with some advanced dynamic data structures like linked lists, trees, graphs, etc., a recursive solution is often much, much, much simpler and easier to understand.

That’s kind of what I was getting at about “rarely” used. While it is true that most people rarely need recursion, when you do need it, you really need it. It’s just that most of us don’t deal with those kinds of data structures.

1 Like

I don’t know if there is a way to offer specific feedback on the courses. In general I really loved the course and appreciate all the thousands of hours that went into creating it and all the work people do here on the forum to support it.

But honestly I think it would be better to replace the recursion exercises at the end of the course with ones that actually force you to use the various loops that we did learn. Rather than an exercise where we aren’t supposed to use the loops we did. As well, I feel like the course didn’t do a good enough job teaching the topic to expect someone to write a recursive function from scratch as was suggested above. Once you understand the concept, for sure the exercise is simple. It’s getting to that understanding that is the issue.

I certainly don’t doubt that recursion is necessary in some situations. But to me it feels like it belongs in a higher level course. With much more explanation.

In a way, the end of the course was kind of a let down because I don’t feel that everything we had learned up to that point prepared us for those two exercises. As someone mentioned above, they had to take an afternoon exploring other material to understand the concept.

But perhaps this isn’t really intended as a beginner beginner class. Although it does start from the absolute basics.

Any way, much appreciation for the course and all the support here. It’s gotten me on my way!

1 Like

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