Skip to content

Instantly share code, notes, and snippets.

@sri-rang
Created December 25, 2011 15:48
Show Gist options
  • Save sri-rang/1519447 to your computer and use it in GitHub Desktop.
Save sri-rang/1519447 to your computer and use it in GitHub Desktop.
Functional Programming in JavaScript - 4
var forEach = function (list, action) {
for (var i = 0; i < list.length; i++) {
action(list[i]);
}
};
var logItem = function (item) {
console.log(item);
};
var listOfThings = ["soap", "candle", "deer", "wine", "bread"];
var anotherListOfThings = ["grapes", "apples", "beer", "pizza"];
forEach(listOfThings, logItem);
forEach(anotherListOfThings, function (item) {
console.log(item + "'s length is " + item.length);
});
@vagon4
Copy link

vagon4 commented Jan 14, 2014

//Great read so far.
//If you would like correct grammar replace the final forEach with:

forEach(anotherListOfThings, function (item) {
if(item[item.length-1]==="s"){
console.log(item + "' length is " + item.length);
}else{
console.log(item + "'s length is " + item.length);
}
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment