Skip to content

Instantly share code, notes, and snippets.

@jalehman
Created November 12, 2015 03:45
Show Gist options
  • Save jalehman/b7100a04e04085265b9b to your computer and use it in GitHub Desktop.
Save jalehman/b7100a04e04085265b9b to your computer and use it in GitHub Desktop.
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