How to negate a predicate function in JavaScript ? - GeeksforGeeks

22 Apr, 2021
In this article, we will see how to negate a predicate function in JavaScript. Predicate functions are the ones that check the condition and return true and false based on argument. Our task is to get the opposite of the predicate function. 
We follow the following method to get the desired result.
Method 1: Our Predicate function is checking for odd and even numbers. If the number is a module with 2, it returns 1 then it is odd, else it is even. Negate the logic while checking conditions for argument.
Example : 
Javascript
<script>  // Predicate function check odd  function isOdd(number) {    return number % 2 == 1;  }  // negation of isOdd function  function isNotOdd(number) {    return number % 2 !== 1;  }  // Predicate function check Even  function isEven(number) {    return number % 2 == 0;  }  // negation of isEven function  function isNotEven(number) {    return number % 2 !== 0;  }    // Outputs: true  console.log(isOdd(5));    // Outputs: true  console.log(isNotOdd(2));    // Output: false  console.log(isEven(3));    // Output> false  console.log(isNotEven(4));</script>
Output: 
true
true 
false
false
Method 2: The problem with the above method is to we are hard-coding our logic at each negation. We probably have more chance to make mistake in negation condition. More effective is to negate predicate function by checking condition.
Example :
Javascript
<script>  // Predicate function check odd  function isOdd(number) {    return number % 2 == 1;  }  // negation of isOdd function  function isNotOdd(number) {    return !isOdd(number);  }  // Predicate function check Even  function isEven(number) {    return number % 2 == 0;  }  // negation of isEven function  function isNotEven(number) {    return !isEven(number);  }    // Outputs: true  console.log(isOdd(5));    // Outputs: true  console.log(isNotOdd(10));    // Output: false  console.log(isEven(3));    // Output> false  console.log(isNotEven(4));</script>
Output : 
true 
true 
false
false
Method 3: In the previous method, we are negating the function for all the predicate functions. But our solution can be more effective if we create one function that negates all the predicate functions. We make predicate function and bind negate function with all predicate functions. 
Example :
Javascript
<script>  // Predicate function check odd  function isOdd(number) {    return number % 2 == 1;  }    // Predicate function check Even  function isEven(number) {    return number % 2 == 0;  }    // function that negate all function  function negate(pre) {    return function (number) {      return !pre(number);    };  }  // Wrapping predicate function to negate function  var isNotOdd = negate(isOdd);  var isNotEven = negate(isEven);    // Outputs: true  console.log(isOdd(5));    // Outputs: true  console.log(isNotOdd(10));    // Output: false  console.log(isEven(3));    // Output: false  console.log(isNotEven(4));</script>
Output : 
true 
true 
false 
false
javascript-functions
JavaScript-Questions
Picked
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Remove elements from a JavaScript Array
Convert a string to an integer in JavaScript
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
How to calculate the number of days between two dates in javascript?
Remove elements from a JavaScript Array
Installation of Node.js on Linux
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?