Overuse of for loops?

Hey guys, i’ve been working through the Intermediate algorithm challenges for the front end cert, and i’m almost finished them all, but one thing i’ve noticed is that for almost all of these challenges i’ve been using the traditional For loop to solve them, and sometimes I worry that I may be over using this feature and that perhaps there is a simpler way to write the code. Sometimes my code will consist of for loops within for loops, and then later another for loop, lol.

Is this considered bad practice? What other things should I be trying? Cause a lot of the challenges are just along the same lines of ‘search through an array… etc’, so it seems like for loops are necessary for almost everything.

1 Like

Using for loops is not bad practice. Using more for loops than needed creates inefficient solutions. That being said, each solution has to be analyzed to see if a more efficient solution can be used. If you have a solution which you think uses to many (extra loops), but not sure, then post some code and ask the question.

Looping (Iterating) through arrays/strings definitely are necessary to solve most of the challenges in some way. Notice I did not say for loop or while loop or some other kind of loop. You use what you feel makes the code most readable. You can also make sure of higher order functions such as map, filter, and reduce to iterate over arrays in particular.

1 Like

Usually the challenge prompt will end with something like “Useful links:” then a list of links to documentation for built-in methods(e.g. Array.prototype.filter(), Array.prototype.map()). I think FCC expects you to use or at the very least understand these.

They can be less tedious than for loops. In addition, some of them help you wrap your head around the idea that functions (or just about anything) can be passed as parameters, which is a very important JS concept.

Ideally you would be proficient at both. You could try doing it with a for loop and then using the methods to make a comparison.

I suppose for loops would be preferable if you don’t actually need to iterate through the whole array and need to cut it short at some point.

For example, to check if a string is a palindrome you could do some combination of splitting the string, dividing the resulting array, reversing one half, joining them back into a string, then comparing them. This solution becomes slower the longer the string is because it will iterate through the array multiple times.

A for loop, on the other hand, can instantly cut the function short and return false once it realizes that the first letter doesn’t match the last letter in the string.
(arr[i] != arr[arr.length - 1 - i]){ return false };

1 Like

I do this as well. I always default to a for loop. but I’ve seen other challenges (Codewars, hackerrank) where other solutions are posted and I realize eventually I can go through and refactor my code with others ways of doing it

Don’t worry about it. Assuming that you’re not making some Byzantine, twisted code, using for loops instead of the Array.prototype methods is largely a stylistic choice. There may sometimes be a slight performance issue, but unless your are dealing with large quantities of data, it won’t matter.

I too defaulted to the trusty for loop while I was learning. As I’ve gotten better, I’ve started using some of those methods. But I think using the for loop for so long to do basically the same thing really helped me be ready for those methods. After all, most of the time, that’s all the methods are, a function with a for loop.

Which is better? Barring arguments of slight performances differences, it’s a matter of style. Is the code clear? Sometimes concision is a good thing. Sometimes we worship concision at the expense of clarity. Sometimes a for loop will be more clear. Sometimes one of the methods will. As you get used to them, I imagine the methods will get clearer. But don’t feel like you need to rush it.

1 Like