Console.log affecting solution

Tell us what’s happening:
I’ve solved the challenge, but the line 8, which I assumed just a console.log, is affecting the response.
When I comment the line 8, my solution pass the test cases.
When I uncomment it, the 3rd test case fails.

Could someone, please, help me understand the reason for this?

Your code so far

Array.prototype.myMap = function(callback) {
  const newArray = [];
  // Only change code below this line
    console.log(`this = ${this}`);
  for(let j = 0;j<this.length;j++){
    console.log(`j = ${j}`);
    newArray.push(callback(this[j],j,this));
    // console.log(`callback(this[j]) = ${callback(this[j])}`);
  }
  console.log(`newArray = ${newArray}`);
  console.log(`-----`);
  // Only change code above this line
  return newArray;
};

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36

Challenge: Functional Programming - Implement map on a Prototype

Link to the challenge:

Line 8 calls callback with one parameter. Maybe try matching the call to the way line 7 does it.

It depends on the callback, that you’re calling, maybe you can share the code of callback?

I’ve tried before, same behavior unfortunately :confused:

I don’t have the code of callback, since it isn’t available at the code editor :confused:

Oops, I’ve changed before only the text-format, but not that which is actually executed. Now it’s solved, thanks!

You do have the code of the callback:

The test case says:

[1, 1, 2, 5, 2].myMap((element, index, array) => array[i + 1] || array[0]) should return [1, 2, 5, 2, 1].

So try:

Array.prototype.myMap = function (callback) {
  const newArray = [];
  // Only change code below this line
  console.log(`this = ${this}`);
  for (let j = 0; j < this.length; j++) {
    console.log(`j = ${j}`);
    newArray.push(callback(this[j], j, this));
    console.log(`callback(this[j]) = ${callback(this[j])}`);
  }
  console.log(`newArray = ${newArray}`);
  console.log(`-----`);
  // Only change code above this line
  return newArray;
};

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

Then the error shows itself.

Of course! So obvious and I wasn’t seeing it, thank you for helping me to see it!

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