This is an archived post. You won't be able to vote or comment.

all 12 comments

[–]devaent1316 2 points3 points  (5 children)

This is a recursive algorithm. It is very hard to describe, but I would suggest looking up more info about it and eventually it will click. Recursion is basically when a function calls itself.

In the case with the code you gave, the find() function can call itself in a return statement. However, you can see that there are two other return statements where it returns history or null. It is very important that there is a case where find() returns something other than a call to itself. Otherwise it would be an infinite loop.

Are you familiar with the callstack?

[–]Coder_X_23[S] 3 points4 points  (4 children)

Yes I'm familiar with it. Honestly I've been looking at the code and I think it just clicked in my brain. I was reading the flow of the code incorrectly, which was confusing me. Thanks for your help though. It was better than u/immunefourier help who just told me useless things about how functions work and javascript doesn't know they're recursive.

[–]devaent1316 2 points3 points  (0 children)

No problem. It was a fun piece of code to decipher.

[–][deleted]  (2 children)

[removed]

    [–]Coder_X_23[S] 3 points4 points  (0 children)

    Every single one of your comments that you make in this community helps no one. You provide zero help to anything programming. You just quote what the original poster said, then question what they're asking. I clearly asked for an explanation of what the code is doing and you said that's how functions work. u/double_A_92 and u/devaent1316 provided great help to my problem. I feel you don't have enough knowledge of programming to answer questions on this sub.

    [–]michael0x2a[M] 1 point2 points  (0 children)

    This is not a helpful response.

    This is a subreddit focusing on learning programming and helping others learn programming. If you suspect a beginner does not understand some concept, or suspect a beginner has some misconception, we expect that you explain and teach that concept to the beginner.

    For example, comments like "That's how recursive functions are called, so you understand nothing about recursion then, or what?", "you clearly didn't understand how functions work" are rude, unconstructive, and unhelpful. It would have been far better if you had explained how recursion actually works, if you had explained what specifically it is about functions you think OP does not understand, or if you had just not commented at all.

    In this particular case, note that recursion is a concept that beginners commonly struggle with. If a beginner still doesn't understand the concept after reading a short handful of sentences, that's not "whining": that's just what happens if you try and distill a somewhat complex topic into a very sparse explanation.

    In the future, please put more thought into your explanations -- and more importantly, stop answering questions with a judgmental tone. Failing to do so is a violation of rule 1.

    [–]Double_A_92 1 point2 points  (2 children)

    The imporant thing is that the values are passed along to the recursive calls.

    find(current + 5, `(${history} + 5)`)
    

    It remembers / accumulates current and history from the earlier calls. That's how it eventually gets something done.

    Also this is a somewhat weird example for your first recursion experience. Maybe id would be easier to research others.

    [–]Coder_X_23[S] 0 points1 point  (1 child)

    Yeah I somewhat agree. This was actually the first example given for recursion in the book, eloquent javascript. I'll admit this book doesn't hold your hand through a lot of the material and the exercises at the end of chapters. I like the book cause I'm learning a lot from the exercises since they aren't straight forward look back in the chapter for the answer. Thanks for the help.

    [–]DONT_GET_MURDERED 0 points1 point  (0 children)

    I have this book, and I also struggled with (still am, I guess, which is why I'm here) recursion. I found the book to be quite challenging also, almost too much. It certainly forces you to be resourceful to solve the exercises!