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.
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;
};
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.
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