How to write a program that takes a predicate and array, like Array.filter(), but only keeps x if pred(x) === false in JavaScript ?

The predicate in JavaScript is the same as the predicate maths which returns true or false.
The predicate is just like a function that determines whether the passed arguments satisfy the given condition or not. The predicate returns only true or false.
In this article, let us see the working of the predicate with arrays when it is passed with a function in JavaScript.
Syntax:
function predicate(argument) 
{
     if(condition_satisfied)
     return true;
     else
     return false;
}
function using_predicate(array, predicate)
{
      for(elements in array )
      {
          if (predicate is satisfied)
          {
            code for satisfied condition;
          }
          else
          {
            code for else condition;
          }
      }
}
Example: The following function takes an array and predicate and returns the resulting array that satisfies pred(x)===false. In this function, the predicate returns true, if the array element is an even number else it returns false. The output should be odd numbers.
Javascript
| <script>     functionpred(x) {         if(x % 2 == 0)             returntrue;         else            returnfalse;     }          functionimplementPredicate(array, pred) {         varres = [];         for(let i = 0; i < array.length; i++) {             if(pred(array[i]) === false) {                 res.push(array[i]);             }         }         returnres;     }     vararray = [1, 2, 2, 3, 4, 5, 6, 6, 7, 8, 8, 8];     varfinal = implementPredicate(array, pred);     console.log("The resultant array is "+final); </script> | 
Output:
The resultant array is 1,3,5,7
Using predicate with filter() method:
Syntax:
function predicate(argument) 
{
     if(condition_satisfied)
     return true;
     else
     return false;
}
array.filter(predicate);
Example: This example uses the filter() method.
Javascript
| <script>     functionpred(x) {         if(x % 2 == 0)             returnfalse;         else            returntrue;     }          vararray = [1, 2, 2, 3, 4, 5, 6, 6, 7, 8, 8, 8];     varfinal = array.filter(pred);     console.log("The resultant array is "+final); </script> | 
Output:
The resultant array is 1,3,5,7
Example: The following function counts the frequency of 8 in the array using a predicate by passing the arguments to the function as the array and the predicate.
Javascript
| <script>     functionpred(x) {         if(x == 8)             returntrue;         else            returnfalse;     }     functionimplementPredicate(array, pred) {         varres = 0;         for(let i = 0; i < array.length; i++) {             if(pred(array[i]) === true) {                 res++;             }         }         returnres;     }               vararray = [1, 2, 2, 3, 4, 5, 6, 6, 7, 8, 8, 8];     varfinal = implementPredicate(array, pred)     console.log("The frequency of 8 is "+ final); </script> | 
Output:
The frequency of 8 is 3
 
				 
					


