Created
November 20, 2024 15:42
-
-
Save pertrai1/7bfb9b7eebd55b058700c658fdf5d0a8 to your computer and use it in GitHub Desktop.
JS Array Prototype - Interview Question
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
/** | |
* Without calling Array.prototype.map(), Array.prototype.filter(), Array.prototype.reduce(), or | |
* Array.prototype.forEach(), implement the following three similar functions on the Array prototype: | |
*/ | |
/* | |
* myMap(callback) | |
* Without mutating the original array, this function should call the passed callback function on every | |
* element of the array and return a new array containing the result of all these calls, in the corresponding order. | |
* The callback function can take up to three parameters: | |
* 1. The current value in the array | |
* 2. The current index in the array | |
* 3. The array itself | |
*/ | |
Array.prototype.myMap = function (callback) {}; | |
/* | |
* myFilter(callback) | |
* Without mutating the original array, this function should call the passed callback function on every element | |
* of the array and return a new array containing the values of the original array that, when passed to the callback | |
* function, returned true. These values should maintain their original order. | |
* | |
* The callback function takes in the same arguments as the one that the callback function in myMap takes in. | |
*/ | |
Array.prototype.myFilter = function (callback) {}; | |
/* | |
* myReduce(callback, initialValue) | |
* Without mutating the original array, this function should call the passed callback function on every element | |
* of the array and return the result of the last call to the callback. | |
* | |
* The callback function can take up to four parameters: | |
* 1. The accumulator, which is the return value of the previous call to the callback. On the first call to the | |
* callback, the accumulator should be set to the intialValue. If the initialValue is undefined, then it should | |
* be set to the first value of the array, and the callback should skip the first element in the array and be | |
* called directly on the second element. | |
* 2. The current value in the array | |
* 3. The current index in the array | |
* 4. The array itself | |
* | |
* If the array contains no elements, the initialValue should be returned. Note that this differs slightly from | |
* the Array.prototype.reduce function. | |
*/ | |
Array.prototype.myReduce = function (callback, initialValue) {}; | |
const arr = [1, 2, 3]; | |
const mappedArray = arr.myMap((value, i, arr) => { | |
return value + i + arr[1]; | |
}); | |
const filteredArray = arr.myFilter((value, i, arr) => { | |
return value + i + arr[1] > 5; | |
}); | |
const reducedValue = arr.myReduce((accumulator, value, i, arr) => { | |
return accumulator + value + i + arr[1]; | |
}, 3); | |
console.log(mappedArray); // [3, 5, 7] | |
console.log(filteredArray); // [3] | |
console.log(reducedValue); // 18 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment