Check out a free preview of the full A Practical Guide to Algorithms with JavaScript course:
The "Looping Review" Lesson is part of the full, A Practical Guide to Algorithms with JavaScript course featured in this preview video. Here's what you'd learn in this lesson:

Bianca reviews the differences and benefit of recursion and a loop.

Get Free Access Now

Transcript from the "Looping Review" Lesson

[00:00:00]
>> Bianca: Okay, so we're looping. We were looping recursively. We're saying that we need to get closer to our base case. In this case, our base case is this condition. And so these are the kind of patterns you should be thinking about when you're formulating your recursive solution. What is our base case?

[00:00:18] How do we get closer? What's the work that we actually care about between now and then? All right, so what's the difference between a recursion and a loop? Not really that much. Recursion can always be implemented as a loop, which is fine. It's not necessarily faster, but there are some situations when recursion is gonna be a lot easier to implement than a loop.

[00:00:47] Because for some complex data structures, especially nested data structures, you don't know ahead of time how deeply nested things are. And to keep track of that, you would have to use an auxiliary data structure that kind of sort of maps to a stack, but you actually use a queue.

[00:01:11] So that's how you would do that. You can do it but it's just way more complicated when you can just do it using recursion. And so that's going to be the typical answer to these recursive data structures like traversing a tree and things like that. The standard answer is gonna be recursion versus, okay, let's make a loop and then the queue and all of this other nonsense.