Created
August 14, 2018 00:27
-
-
Save Samuelachema/552cc2eec057973313175935c4fbf31e 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