Last active
November 1, 2024 12:00
-
-
Save alexhawkins/28aaf610a3e76d8b8264 to your computer and use it in GitHub Desktop.
Implementation of Native JavaScript Methods (forEach, Map, Filter, Reduce, Every, Some)
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
'use strict'; | |
/*****************NATIVE forEACH*********************/ | |
Array.prototype.myEach = function(callback) { | |
for (var i = 0; i < this.length; i++) | |
callback(this[i], i, this); | |
}; | |
//tests | |
var arr = ['biggy smalls', 'bif tannin', 'boo radley', 'hans gruber']; | |
arr.myEach(function(word) { | |
console.log(word); | |
}); | |
//biggy smalls | |
//bif tannin | |
//boo radley | |
//hans gruber | |
/*****************NATIVE MAP*************************/ | |
Array.prototype.myMap = function(callback) { | |
arr = []; | |
for (var i = 0; i < this.length; i++) | |
arr.push(callback(this[i], i, this)); | |
return arr; | |
}; | |
//tests | |
var arrs = ['dic tanin', 'boo radley', 'hans gruber']; | |
var numbers2 = [1, 4, 9]; | |
var goodT = arrs.myMap(function(n) { | |
return n; | |
}); | |
var squareRoot = numbers2.myMap(function(num) { | |
return Math.sqrt(num); | |
}); | |
console.log(goodT); // [ 'dic tanin', 'boo radley', 'hans gruber' ] | |
console.log(squareRoot); // [ 1, 2, 3 ] | |
/*****************NATIVE FILTER*************************/ | |
Array.prototype.myFilter = function(callback, context) { | |
arr = []; | |
for (var i = 0; i < this.length; i++) { | |
if (callback.call(context, this[i], i, this)) | |
arr.push(this[i]); | |
} | |
return arr; | |
}; | |
//tests | |
var numbers = [1, 20, 30, 80, 2, 9, 3]; | |
var newNum = numbers.myFilter(function(n) { | |
return n >= 10; | |
}); | |
console.log(newNum); // [ 20, 30, 80 ] | |
/*****************NATIVE REDUCE*************************/ | |
Array.prototype.myReduce = function(callback, initialVal) { | |
var accumulator = (initialVal === undefined) ? undefined : initialVal; | |
for (var i = 0; i < this.length; i++) { | |
if (accumulator !== undefined) | |
accumulator = callback.call(undefined, accumulator, this[i], i, this); | |
else | |
accumulator = this[i]; | |
} | |
return accumulator; | |
}; | |
//tests | |
var numbers3 = [20, 20, 2, 3]; | |
var total = numbers3.myReduce(function(a, b) { | |
return a + b; | |
}, 10); | |
console.log(total); // 55 | |
var flattened = [ | |
[0, 1], | |
[2, 3], | |
[4, 5] | |
].reduce(function(a, b) { | |
return a.concat(b); | |
}); | |
console.log(flattened); //[ 0, 1, 2, 3, 4, 5 ] | |
/*****************NATIVE EVERY*************************/ | |
Array.prototype.myEvery = function(callback, context) { | |
for (var i = 0; i < this.length; i++) { | |
if (!callback.call(context, this[i], i, this)) | |
return false; | |
} | |
return true; | |
}; | |
//tests | |
var passed = [12, 5, 8, 130, 44].myEvery(function(element) { | |
return (element >= 10); | |
}); | |
console.log(passed); // false | |
passed = [12, 54, 18, 130, 44].myEvery(function(element) { | |
return (element >= 10); | |
}); | |
console.log(passed); // true | |
passed = [12, 54, 18, 130, 44].myEvery(function(element) { | |
return (element >= 13); | |
}); | |
console.log(passed); // false | |
/*****************NATIVE SOME*************************/ | |
Array.prototype.mySome = function(callback, context) { | |
for (var i = 0; i < this.length; i++) { | |
if (callback.call(context, this[i], i, this)) | |
return true; | |
} | |
return false; | |
}; | |
//tests | |
var passed = [12, 5, 8, 130, 44].mySome(function(element) { | |
return (element >= 200); | |
}); | |
console.log('some: ' + passed); //some: false | |
var passed = [12, 5, 8, 130, 44].mySome(function(element) { | |
return (element >= 100); | |
}); | |
console.log('some: ' + passed); //some: true |
@alexhawkins Bro, what will happen to your methods which are binding context if you pass arrow function () => {}. I do think it is not gonna work.
I deleted my smiles because I thought specific bug in your implementation, but js filter works as same as yours. So I guess thats why none uses it. 😅 This one I had to add.
`Array.prototype.myFilter = function(callback, context) {
arr = [];
for (var i = 0; i < this.length; i++) {
if (callback.call(context, this[i], i, this))
arr.push(this[i]);
}
return arr;
};
[].myFilter.call([1,2,3], (e) => {
console.log(this.isItWorking)
return e % 2 === 0
}, {isItWorking: "mmmm..."})`
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
else
accumulator = this[i]; at line 70
is not like this in current native reduce,
this one will omit undefined values at any step and proceed to this[i]
the native one will sum it up, it will use this check for initial value just once
Thank you!