Loading collection data...
Collections are a way for you to organize kata so that you can create your own training routines. Every collection you create is public and automatically sharable with other warriors. After you have added a few kata to a collection you and others can train on the kata contained within the collection.
Get started now by creating a new collection.
@dansel I get your point, you can approach the kata in whichever way you want as long as it's not forbidden. But the problem is your solution is voted as best practice, which certainly isn't. That's why people protest.
You could use
.unwrap_or(0)
rather than pattern matching.very elegant, however will be too slow given a couple of million array elements.
'filter' creates a new array + iterates through the total array.
the 'reduce' solution stops when needed and returns a boolean.
Thanks for your reply.
Since speed is not mentioned in the kata description I did not care about execution time.
It's a 7 kyu kata and it's "fast enough".
This solution is KISS and easy on the eyes though.
One could of course implement faster solutions with "fail fast".
Slow
This is a common trend in codewars. The most upvoted solutions tend to be shortest rather than the most efficient.
awesome
This comment is hidden because it contains spoiler information about the solution
Using filter or forEach, while more succinct, is less efficient for this sort of thing, since these functions will continue until the entire array has been iterated, whereas a normal loop (or functions like every/some) can be broken as soon as an element is found that does not meet the criteria.
Didn't know about filter! Thanks!