Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //forEach
- Array.prototype.myforEach = function(cb) {
- for (let i = 0; i < this.length; i++){
- cb(this[i], i, this);
- }
- };
- //map
- Array.prototype.myMap = function(cb) {
- let newArr = [];
- for (let i = 0; i < this.length; i++){
- newArr.push(cb(this[i], i, this));
- }
- return newArr;
- };
- //filter
- Array.prototype.myFilter = function(cb) {
- let newArr = [];
- for (let i = 0; i < this.length; i++) {
- if (cb(this[i], i, this)){
- newArr.push(this[i]);
- }
- }
- return newArr;
- };
- //some
- Array.prototype.mySome = function(cb) {
- for (let i = 0; i < this.length; i++) {
- if (cb(this[i], i, this)){
- return true;
- }
- };
- return false;
- };
- //every
- Array.prototype.myEvery = function(cb) {
- for (let i = 0; i < this.length; i++) {
- if (!cb(this[i], i, this)){
- return false;
- }
- };
- return true;
- };
- //reduce
- Array.prototype.myReduce = function(cb, initialVal) {
- var accumulator = (initialVal === undefined) ? undefined : initialVal;
- for (var i = 0; i < this.length; i++) {
- if (accumulator !== undefined){
- accumulator = cb.call(undefined, accumulator, this[i], i, this);
- } else {
- accumulator = this[i];
- }
- }
- return accumulator;
- };
Add Comment
Please, Sign In to add comment