Skip to content

Instantly share code, notes, and snippets.

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