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.
like you said, if two words have the same length.
reduce
will compare them by index. The first to appear will be called. In this casereason
Theres an error in your code test
Test.assertEquals(longestString('now for some reason my shoe smells funny'), "reason", "try again");
the string lenth of reason & smells equals the same
reason.length === smells.length
so reason can't be the longest word by it's lone some. You should create a test that responds to the case and have coders also check for the edge case. Happy Coding
Out of curiosity I ran a performance test comparing normal division with the division by bitwise shift in this case:
https://jsperf.com/numsum-division
The bitwise division is a little faster.
Very clever. I like the use of the shift operator to do the division.
Very cool, thanks!
Just an alternative to shorten it a bit.
The two cases are:
The string has an even number of characters
The string has an odd number of characters.
If the string has an even number of characters we start the slicing one position early.
We can subtract !(str.length % 2) from the starting position, str.length / 2.
!(str.length % 2) === 1 if even, 0 if odd
Little math trick:
The sum of the first n natural numbers is equal to n(n + 1)/2