The longest word
- O(n) · Medium
- Full plan
- Python
- JavaScript
- strings
- lists
- loops
Problem
Write a function that takes a phrase and returns its longest word. The words come separated by one space, with no commas or periods.
If two words are the same length, you return the first of them: with "cat dog" you return "cat".
If the phrase is empty, you return an empty text.
Examples
"the cat sleeps" → "sleeps"
A tie, the first one wins
"cat dog" → "cat"
A single word
"python" → "python"
Besides these, the challenge has hidden tests that are revealed when you submit your solution.
You start with this
Python
def longest_word(phrase):
passJavaScript
function longestWord(phrase) {
}Solve this challenge
It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.