Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to test a value x against predicate function and returns fn(x) or x in JavaScript ?

  • Last Updated : 20 Jul, 2021

Given a value x, and the task is to check the value against predicate function. If the value satisfies the condition or predicate return fn(x) else return x.

Predicate functions are the function that takes one item as input and returns true or false based on the whether item satisfies that condition or not.

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

Example 1: In this example, we have given a number an a condition, if condition is true then it returns the fn(number) otherwise it returns simple number value.

Condition : isEven 
Input : 6
Output : 720 

Explanation : Here condition is number that should 
              be even because our input data is 
              even so it returns the fn(6) value.

Code Implementation:  



Javascript




<script>
 
    // Function that we have to return
    function fn(x) {
        let fact = 1;
        for (i = 1; i <= x; i++) {
            fact *= i;
        }
 
        return fact;
    }
 
    // Predicate function checking even value
    function isEven(x) {
        return (x % 2 === 0);
    }
 
 
    // Function returning fn(x) if predicate
    // function return true else return x
    function test(x) {
        let e = isEven(x);
 
        if (e) {
            return fn(x);
 
        } else
            return x;
    }
 
    // Printing return value
    let ans = test(6);
    console.log(`This function returns ${ans}`);
</script>


Output: 

This function returns 720

Example 2: In this example, we have given a number and a test condition, if condition is true then it returns the fn(number) otherwise it returns number value only. 

Condition : x less than max_value
Input : 15 
Output : 610

Code Implementation: 

Javascript




<script>
 
    // Initializing the max_Iteger
    let max_Iteger = 20;
 
    // Function we have to return
    function fn(x) {
        var b = 0;
        var a = 1;
        var sum;
        for (i = 0; i < x - 1; i++) {
            sum = a + b;
            b = a;
            a = sum;
        }
        return a;
    }
 
    // Predicate function checking value
    // is less than max_Iteger
    function isless(x) {
        return (x < max_Iteger);
    }
 
    // Function return fn(x) if predicate
    // function return true else return x
    function test(x) {
        let e = isless(x);
 
        if (e) {
            return fn(x);
 
        } else
            return x;
    }
 
    // Printing return value
    let ans = test(15);
    console.log(`This function returns ${ans}`);
</script>


Output: 

The function returns 610

Example 3: In this example, we have given a number and a test condition, if condition is true then it returns the fn(number) otherwise it returns number value only. 

Condition : x is multiple of 3
Input : 6
Output : 7776

Code Implementation:  

Javascript




<script>
 
    // Function we have to return
    function fn(x) {
 
        let j = 1;
        for (i = 0; i < x - 1; i++) {
            j = j * x;
        }
        return j;
    }
 
    // Predicate function checking
    // multiple of 3
    function ismultiple(x) {
        return (x % 3 == 0);
    }
 
    // Function that returns fn(x) if
    // predicate function return true
    // else return false
    function test(x) {
        let e = ismultiple(x);
 
        if (e) {
            return fn(x);
 
        } else
            return x;
    }
 
    // Printing value
    let ans = test(6);
    console.log(`This function returns ${ans}`);
</script>


Output: 

This function returns 7776

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :