Native Implementation
ForEach
// Some code
/*******************************************************************************
Write a function `myForEach` that accepts an array and a callback as arguments.
The function should call the callback on each element of the array, passing in the
element, index, and array itself. The function does not need to return any value.
Do not use the built in Array#forEach.
Examples:
myForEach(['a', 'b', 'c'], function (el, i) {
console.log(el + ' is at index ' + i);
}); // prints
// a is at index 0
// b is at index 1
// c is at index 2
let test = [];
myForEach(['laika', 'belka'], function (el) {
test.push(el.toUpperCase());
});
console.log(test); // ['LAIKA', 'BELKA']
*******************************************************************************/
function myForEach(array, cb) {
for (let i = 0; i < array.length; i++) {
let el = array[i];
cb(el, i, array);
}
}
module.exports = myForEach;Map
Reduce
Array Callback Methods Implemented With For Loops
Array Callback Methods Implemented With For Loops
Functions are called โFirst Class Objectsโ in JavaScript because:
Further Examples of the above concepts
Last updated