Skip to content

Instantly share code, notes, and snippets.

@Samuelachema
Created August 14, 2018 00:27
Show Gist options
  • Save Samuelachema/552cc2eec057973313175935c4fbf31e to your computer and use it in GitHub Desktop.
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
/*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