Created
August 14, 2018 00:27
-
-
Save Samuelachema/6b1aa8f941a589347e38476af3b738eb to your computer and use it in GitHub Desktop.
Write a function called longest which will take a string of space separated words and will return the longest one
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*Write a function called longest which will take a string of space separated words and will return the longest one. | |
For example: | |
longest("This is Andela") | |
returns | |
"Andela" | |
and | |
longest("Brilliance is evenly distributed") | |
returns | |
distributed | |
*/ | |
function longest(sentence){ | |
let str = sentence.split(" "); | |
let longest = 0; | |
let word = null; | |
str.forEach(function(str) { | |
if (longest < str.length) { | |
longest = str.length; | |
word = str; | |
} | |
}); | |
return word; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment