I am trying to mimic 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];
//implement this line
var doubles = map(numbers, function (number) {
return number * 2;
});
console.log(doubles); // [2, 4, 6]
So, I have this numbers
array with values and I have my own map function
to iterate through the array and then I pushing the values in an empty array empMap
later, I am calling this map function and multiplying each value with 2 and printing it out.
I should get the output of doubles as [2, 4, 6] instead I am getting [1,2,3]. Not sure where I going wrong.