Skip to content

Instantly share code, notes, and snippets.

@troyleach
Created March 27, 2015 22:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save troyleach/5aa193515b9425faeef8 to your computer and use it in GitHub Desktop.
Save troyleach/5aa193515b9425faeef8 to your computer and use it in GitHub Desktop.
Find the longest word in a phase
// Write a function findLongestWord() that takes an array of words and returns the length of the longest one.
// woops, didn't read it.... I took a phase turned it into a array...
var longestWord = "";
function findLongestWord(phase) {
var sentence = phase.split(/\s/);
for ( var i = 0; i < sentence.length; i++ ) {
if (sentence[i].length > longestWord.length) {
longestWord = longestWord.replace(longestWord, sentence[i]);
}
}
return longestWord;
}
console.log(findLongestWord("find the longest word in this phase"));
var biggestWord = 'longest'
console.log(biggestWord === longestWord)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment