Array.prototype.myForEach = function(fn) {
for (const item of this) {
fn(item);
}
}
Array.prototype.myMap = function(fn) {
const res = [];
for (const item of this) {
res.push(fn(item));
}
return res;
}
Array.prototype.myFind = function(fn) {
for (const item of this) {
if (fn(item)) return item;
}
return undefined;
}
Array.prototype.myFilter = function(fn) {
const res = [];
for (const item of this) {
if (fn(item)) res.push(item);
}
return res;
}
Array.prototype.mySome = function(fn) {
for (const item of this) {
if (fn(item)) return true;
}
return false;
}
Array.prototype.myEvery = function(fn) {
for (const item of this) {
if (!fn(item)) return false;
}
return true;
}
const arr = [1, 2, 3, 4, 5];
arr.myForEach((item) => {
console.log(item);
});
const doubledArray = arr.myMap((item) => {
return item * 2;
});
console.log(doubledArray);
const foundItem = arr.myFind((item) => {
return item > 2;
});
console.log(foundItem);
const filteredArray = arr.myFilter((item) => {
return item % 2 === 0;
});
console.log(filteredArray);
const hasEvenNumber = arr.mySome((item) => {
return item % 2 === 0;
});
console.log(hasEvenNumber);
const allEvenNumbers = arr.myEvery((item) => {
return item % 2 === 0;
});
console.log(allEvenNumbers);