Skip to content

Instantly share code, notes, and snippets.

@d3vild06
Last active July 1, 2016 15:51
Show Gist options
  • Save d3vild06/7f072adfa85baa0fe125fafd44fab1d0 to your computer and use it in GitHub Desktop.
Save d3vild06/7f072adfa85baa0fe125fafd44fab1d0 to your computer and use it in GitHub Desktop.
Find Shortest String In Array
var my_arry = ["hello", "muchasgracias", "hi", "elephant"];
function findShortest(arry) {
return arry.reduce(function(prevWord,currWord) {
if (currWord.length < prevWord.length) {
return currWord;
}
else
return prevWord;
})
/* reduce process
prevWord currWord prevWord.length currWord.length currWord < prevWord ? return value (currWord || prevWord)
"hello" "muchasgracias" 5 13 no "hello"
"hello" "hi" 5 2 yes "hi"
"hi" "elephant" 2 8 no "hi"
*/
}
findShortest(my_arry);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment