Callback not a function error?

Tell us what’s happening:
Trying to understand why I am getting callback not a function error below?

Your code so far


// the global Array
var s = [23, 65, 98, 5];

Array.prototype.myMap = function(callback){
  var newArray = [];
  // Add your code below this line
  
  newArray = this.slice();
  newArray.forEach( item => callback(item) ); //callback is not a function error?

  // Add your code above this line
  return newArray;

};

var new_s = s.myMap(function(item){
  return item * 2;
});

//console.log(s);
//console.log( s.map( item=>item*5 ) );
console.log( s.myMap( new_s ) );

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/functional-programming/implement-map-on-a-prototype

I also tried the ‘solution’, but I m getting the same callback function error. I am using google chrome. Any ideas?

Array.prototype.myMap = function(callback){
var newArray = [];
// Add your code below this line
this.forEach(a => newArray.push(callback(a)));
// Add your code above this line
return newArray;
};

After I comment out this line, the error goes away:

console.log( s.myMap( new_s ) );

Also fixed the code because I did not realize forEach doesn’t update the array directly:

newArray = this.slice();
newArray.forEach( (t, i, a)=>a[i]=callback(t) );

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

new_s is not a function, but it is passed as the parameter callback that you used to build the method, so when you try to use it as a function it will not work and grow an error

To check it do instead

console.log( new_s );

Does your code works now?

Because it’s not a function, it’s an array that you’re passing to s.myMap.

new_s is [46, 130, 196, 10], so you’re trying to run [23, 65, 98, 5].myMap([46, 130, 196, 10])

My bad. Did not realize it. Thanks.