Negate a predicate
You have a predicate and you need its opposite. Writing items.filter((x) => !isArchived(x)) works, but once you are composing filters you want the negated predicate as a value you can pass around and name.
Implement negate(predicate). It returns a new function that forwards every argument, and its this, to predicate, and returns the boolean opposite of the result. The return value is always a real boolean: a truthy result such as the string "hello" becomes false, and a falsy result such as 0 becomes true.
What it has to do
- Return
truewhen the predicate returns a falsy value, andfalsewhen it returns a truthy one. - Always return an actual boolean, never the negated operand itself.
- Forward every argument the wrapper receives to the predicate.
- Preserve
thisso a negated predicate works as an object method.
Your workspace
Ready to check it?
5 tests run against your code, right here in your browser. Sign in to claim the XP when you pass.
AI Crack & Solution Assist
Stuck? Get instant AI hints or break down the optimal solution.
Stuck? The javascript course covers everything this challenge needs.