Last active
September 10, 2016 21:56
replaces for loop with each function
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
/* 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