Replacing some for loops by filter()
Replacing loop by filter() and map()
Replacing loop with simpler length comparison