Skip to content

Instantly share code, notes, and snippets.

@dengjonathan
Last active September 10, 2016 21:56
Show Gist options
  • Save dengjonathan/5ade6b03f4ed135caed2f25f634eeee0 to your computer and use it in GitHub Desktop.
Save dengjonathan/5ade6b03f4ed135caed2f25f634eeee0 to your computer and use it in GitHub Desktop.
replaces for loop with each function
/* how do you encapsulate a for loop within a function
so that each(names, callback) will do the same thing as the for loop? */
// ensure that the all inputs are explicity declared in function signature
var each = function(array, callback) {
for (var i = 0; i < array.length; i++) {
callback(array[i]);
}
};
each(names, log); // console logs each item in array without creating a variable named i in global namespace
each([1, 2, ])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment