Open In App

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

Last Updated : 03 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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>
    function pred(x) {
        if (x % 2 == 0)
            return true;
        else
            return false;
    }
      
    function implementPredicate(array, pred) {
        var res = [];
        for (let i = 0; i < array.length; i++) {
            if (pred(array[i]) === false) {
                res.push(array[i]);
            }
        }
        return res;
    }
    var array = [1, 2, 2, 3, 4, 5, 6, 6, 7, 8, 8, 8];
    var final = 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>
    function pred(x) {
        if (x % 2 == 0)
            return false;
        else
            return true;
    }
      
    var array = [1, 2, 2, 3, 4, 5, 6, 6, 7, 8, 8, 8];
    var final = 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>
    function pred(x) {
        if (x == 8)
            return true;
        else
            return false;
    }
    function implementPredicate(array, pred) {
        var res = 0;
        for (let i = 0; i < array.length; i++) {
            if (pred(array[i]) === true) {
                res++;
            }
        }
        return res;
    }
      
      
    var array = [1, 2, 2, 3, 4, 5, 6, 6, 7, 8, 8, 8];
    var final = implementPredicate(array, pred)
    console.log("The frequency of 8 is " + final);
</script>


Output:

The frequency of 8 is 3


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads