Friday, September 20, 2024
Google search engine
HomeLanguagesJavascriptHow to write a program that takes a predicate and array, like...

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>
    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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments