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.
This comment is hidden because it contains spoiler information about the solution
That is actually pretty clever.
map is a sugar of reduce~
what a map
mind to explain a bit?
Interesting solution
Nice! I have a similar solution using reduce, but this is even easier.
No early return by using
map
if it finds the missing number.There're some comments about
forEach
, it doesn't early return, too.A really clever solution. One downside is that there is no early return, meaning that it will loop over every single value in what could be a very large array, even if a missing number has already been identified.
Yes,
forEach
would be more appropriate in this instance as the iteration doesn't need to return anything.I did mine like in the top rated example but I like this more.
for
is more suitable I think.i starts as the char code of the first entry, it will increment if it matches in the .map to be the one more (the i++). If it doesnt match at any point then there is a missing letter, and i will stop increasing which will leave i as the char code of the missing letter.
It's useful to go along with pen and paper to see what each operation is doing. Remember that i++ mutates i, and notice that the map is just acting as a forEach here
Yeah. He isn't storing or using the array
.map
creates. Just iterating over it to conditionally incrementi
. So that's why I asked why he used.map
and not.forEach
forEach: This iterates over a list and applies some operation with side effects to each list member (example: saving every list item to the database)
map: This iterates over a list, transforms each member of that list, and returns another list of the same size with the transformed members (example: transforming list of strings to uppercase)
ref: https://stackoverflow.com/questions/34426458/javascript-difference-between-foreach-and-map
I have a hard time understanding the ternary operator usage here. Could someone please explain this solution in more depth? It just seems so sharp and to the point! I really want to understand this better
Loading more items...