Created
November 12, 2015 03:45
-
-
Save jalehman/b7100a04e04085265b9b to your computer and use it in GitHub Desktop.
This file contains 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 longerThan(word1, word2) { | |
if (word2.length > word1.length) { | |
return word2; | |
} else { | |
return word1; | |
} | |
} | |
function reduce(array, f, acc) { | |
var start = acc; | |
for(var i = 0; i < array.length; i++) { | |
start = f(start, array[i]); | |
} | |
return start; | |
} | |
function map(array, f) { | |
var temp = []; | |
for(var i = 0; i < array.length; i++) { | |
temp.push(f(array[i])); | |
} | |
return temp; | |
} | |
map([1,2,3,4,5], function(x) { return x * x;}); | |
function longestWord(sentence) { | |
var words = sentence.split(" "); | |
return reduce(words, longerThan, words[0]); | |
} | |
longestWord("the quick brown fox jumped over the lazy dog"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment