Last active
August 29, 2015 14:22
-
-
Save innerspirit/6206340409b3488a004f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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