Skip to content

Instantly share code, notes, and snippets.

@innerspirit
Last active August 29, 2015 14:22
Show Gist options
  • Save innerspirit/6206340409b3488a004f to your computer and use it in GitHub Desktop.
Save innerspirit/6206340409b3488a004f to your computer and use it in GitHub Desktop.
function LongestWord(sen) {
if (typeof LongestWord.cache === 'undefined') {
LongestWord.cache = [];
} else {
if (LongestWord.cache.hasOwnProperty(sen)) {
return LongestWord.cache[sen];
}
}
LongestWord.cache[sen] = sen
.match(/[\w]+/g)
.sort(function(a,b){return b.length - a.length})
.shift();
return LongestWord.cache[sen];
}
console.log(LongestWord('one two three'));
console.log(LongestWord('three one two'));
console.log(LongestWord('two one three'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment