← Back to challenges

Longest Word

JavaScriptHardarrayslanguage_fundamentalsloopsstrings

Instructions

Write a function that finds the longest word in a sentence and returns it. If two or more words are the biggest, return the word closest to the start of the sentence. Characters such as apostrophe, commas, periods, etc count as letters (e.g. O'Connor is 8 characters long).

Examples

longestWord("Hello darkness my old friend") ➞ "darkness"

longestWord("Hello there mate") ➞ "Hello"

longestWord("Kayla's toy is plastic") ➞ "Kayla's"

Notes

N/A

javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.
Next: RegEx: Character Classes XII⁠I - hexidecimals