Can u tell me what is the function of (callback) and (x). Are they variables or object, method , parameter?

Tell us what’s happening:

Your code so far


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

Array.prototype.myFilter = function(callback){
  var newArray = [];
  // Add your code below this line
  this.forEach(function(x) {
    if (callback(x) == true) {
      newArray.push(x);
    }
  })
  // Add your code above this line
  return newArray;

};

var new_s = s.myFilter(function(item){
  return item % 2 === 1;
});

Your browser information:

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

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

‘callback’ is a… callback function , myFilter is a higher order function. x represents an element of an array that forEach operates on.

function(x) {
    if (callback(x) == true) {
      newArray.push(x);
    }
  }

^^ This is a callback that gets passed as a parameter when calling forEach on an array. Honestly the examples of forEach on MDN are easy to understand, try looking there if you don’t understand what x is. You can also try reading this explanation:

1 Like