Skip to content

Instantly share code, notes, and snippets.

Created July 31, 2016 19:50
Show Gist options
  • Save anonymous/23a2d2cdacea2088f2aa06a01f895bf0 to your computer and use it in GitHub Desktop.
Save anonymous/23a2d2cdacea2088f2aa06a01f895bf0 to your computer and use it in GitHub Desktop.
https://repl.it/CgPo/37 created by sethopia
// Attach a method to the Array prototype called myForEach.
// It should be utilized and perform exactly as Array.prototype.forEach does. Use the code snippet below as a guide.
Array.prototype.myForEach = function(callback) {
for (var i = 0; i < this.length; i++) {
callback(this[i],i,this);
}
}
var sum = 0;
function addToSum(num) {
sum += num;
}
var arr = [1,2,3];
arr.myForEach(addToSum);
console.log(sum); // 6
Native Browser JavaScript
>>> 6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment