Implement map on a Prototype - Console.Log changing output

Apologies if I paste the code incorrectly, I am not sure the proper format for pasting code on forums.

Anyways, during the lesson called Implement map on a Prototype, I finished it with the following code.

Array.prototype.myMap = function(callback) {
const newArray = ;
// Only change code below this line

for(let i = 0; i <= this.length - 1; i++){

// console.log(newArray.push(callback(this[i], i, this)))

newArray.push(callback(this[i], i, this))

}
// Only change code above this line
return newArray;
};

console.log([1, 1, 2, 5, 2].myMap((element, index, array) => array[index + 1] || array[0]));

This passes the test for me and my uncommented console.log gives me a result of [ 1, 2, 5, 2, 1 ]. However, if I uncomment my commented console.log “console.log(newArray.push(callback(this[i], i, this)))” I get an answer of [ 1, 1, 2, 2, 5, 5, 2, 2, 1, 1 ] from the last console.log line of code and Run the Tests says that my answer is incorrect. What is going on here? Is console.log affecting my output?

Yes. You might assume that anything inside a console.log is just “looking” at some value and not really doing anything, but if it has to be evaluated it has to be run to do so. So a push in a console.log will still push those values to wherever you told it.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.