Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sethschori/b621e6306be3792842d4f9b0ae5ea5fd to your computer and use it in GitHub Desktop.
Save sethschori/b621e6306be3792842d4f9b0ae5ea5fd 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