Hone logo
Hone
Problems

Implementing Array.prototype.map in JavaScript

The Array.prototype.map() method is a fundamental function in JavaScript used for transforming arrays. It allows you to create a new array by applying a provided function to each element of the original array. Implementing this method yourself is a great exercise in understanding array manipulation and functional programming concepts.

Problem Description

Your task is to implement a custom version of Array.prototype.map() that mirrors the behavior of the built-in JavaScript map() method. This custom myMap() function should take an array and a callback function as arguments. The callback function will be applied to each element of the array, and the results of these calls will be collected into a new array, which will then be returned. The original array should not be modified.

Key Requirements:

  • New Array Creation: The function must return a new array.
  • Callback Execution: The provided callback function must be executed for each element in the input array.
  • Argument Handling: The callback function should receive three arguments: the current element, the index of the current element, and the original array itself (in that order).
  • Return Value: The return value of the callback function for each element should be used as the corresponding element in the new array.
  • Immutability: The original array must remain unchanged.

Expected Behavior:

The myMap() function should behave identically to the built-in map() method in terms of applying the callback and constructing the new array.

Edge Cases to Consider:

  • Empty Array: If the input array is empty, the function should return a new, empty array.
  • Null or Undefined Input: Handle cases where the input array is null or undefined gracefully (e.g., by returning an empty array or throwing an error – specify your chosen behavior in your solution).
  • Callback Function: Ensure the callback function is actually a function. If it's not, handle it appropriately (e.g., by throwing an error or returning the original array).
  • Non-Array Input: Handle cases where the input is not an array.

Examples

Example 1:

Input: [1, 2, 3], function(x) { return x * 2; }
Output: [2, 4, 6]
Explanation: The callback function doubles each element of the input array.

Example 2:

Input: ["a", "b", "c"], function(x, i) { return x.toUpperCase() + i; }
Output: ["A0", "B1", "C2"]
Explanation: The callback function converts each element to uppercase and appends its index.

Example 3:

Input: [], function(x) { return x + 1; }
Output: []
Explanation: An empty array is passed as input, so an empty array is returned.

Example 4:

Input: [1, 2, 3], function(x) { return x * 2; }
Output: [2, 4, 6]
Explanation: Demonstrates a simple mapping operation.

Constraints

  • The input array can contain any type of data.
  • The callback function can accept up to three arguments (element, index, original array).
  • The function must not modify the original array.
  • The function should be performant for arrays of reasonable size (up to 10,000 elements).

Notes

  • Consider using a for loop or while loop to iterate through the array.
  • Remember to create a new array to store the results.
  • Think about how to handle potential errors or invalid input.
  • The goal is to replicate the core functionality of Array.prototype.map(). You don't need to implement all the advanced features of the built-in method (e.g., handling this context).
Array.prototype.myMap = function(callback) {
  // Your code here
};
Loading editor...
javascript