Attempting to replicate the functionality of array.map()
using forEach().
var map = function () {
var empMap = [];
numbers.forEach(function (number) {
empMap.push(number);
console.log('empMap', empMap);
});
return(empMap);
}
var numbers = [1, 2, 3];
//need to implement this part
var doubles = map(numbers, function (number) {
return number * 2;
});
console.log(doubles); // [2, 4, 6]
The scenario involves an array named numbers
with specific values and a custom map function
designed to traverse through the array, storing the values in an empty array called empMap
.
Subsequently, the map function is invoked to multiply each value by 2 and display the result.
The expected output for doubles is [2, 4, 6], however, the actual outcome is [1, 2, 3]. The cause of this discrepancy is currently unknown.