Functional Programming - Implement the filter Method on a Prototype

Tell us what’s happening:
Hi everyone!! Pls help))


where is my mistake???

Your code so far

Array.prototype.myFilter = function(callback) {
  const newArray = [];
  // Only change code below this line
for(let i=0;i<this.length;i++){
  if(callback(this[i])===true) {newArray.push(this[i])}
  
}
  // Only change code above this line
  return newArray;
};

// Test case
const s = [23, 65, 98, 5, 13];
const odd_s = s.myFilter(item => item % 2 === 1);
console.log(odd_s)

Your browser information:

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

Challenge: Functional Programming - Implement the filter Method on a Prototype

Link to the challenge:

it’s working when i remove the “===true”. Why???

Try this

console.log(
  [23, 65, 98, 5, 13].myFilter(item => item % 2)
)

What do you see? What is the return value of the callback here?

yep, i see [], but what the reason??
if(callback(a)) and if(callback(a)===true) is equal.

Not quite.

if (5) console.log('hi')
if (5 === true) console.log('nah')

Did you see what I was getting at here?

Filter cares about truthiness, not if the callback returns the Boolean literal true

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