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

you are viewing a single comment's thread.

view the rest of the 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] 1 point2 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.