Tell us what’s happening:
Hello everyone, I have a bit of issue here. I’ve wrote the for loop version but the output of new_s
is the same as s
which is [23, 65, 98, 5]
. not sure why?
the value of newArray.push(this[i]);
is [23, 65, 98, 5]
I think this code should work just fine, but it’s not. I’m not sure what I’m missing. please enlight me.
Your code so far
// The global variable
const s = [23, 65, 98, 5];
Array.prototype.myMap = function(callback) {
const newArray = [];
// Only change code below this line
for (let i = 0; i< this.length; i++){
newArray.push(this[i]);
}
// Only change code above this line
return newArray;
};
const new_s = s.myMap(function(item) {
return item * 2;
});
console.log(new_s);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36
Challenge: Functional Programming - Implement map on a Prototype
Link to the challenge: