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


Testing the value x against the predicate function means checking if x is evaluated to be valid for a particular condition. If x is following the particular condition, we need to perform some operation on the x using any function named ‘fn’ and passing it as a function parameter. Otherwise, we need to return the value of x itself.

Syntax

Users can follow the syntax below to test a value x against the predicate function and return the fn(x) or x in JavaScript.

let result = predicate(x) ? operation(x) : x; 

In the above syntax, we have used the ternary operator to test a value against the predicate() function and return operation(x) or x value based on the returned value from the predicate() function.

Example

In the example below, we have created the operation() function, which returns the square of the number. The predicate() function checks if the value of x is greater than 5 or not and returns true or false based on the condition.

While assigning the value to the result variable, we check if x passes the test of the predicate() function. If yes, we assign the returned value from the operation() function to the ‘result’ variable; Otherwise, x itself.

<html>
<body>
   <h3>Testing a value x <i> against predicate function and returns fn(x) or x </i> in JavaScript</h3>
   <div id = "content"> </div>
   <script>
      let content = document.getElementById("content");
      let x = 10;
      function operation(x) {
         return x * x; 
      }
      function predicate(x) {
         return x > 5;
      }
      let result = predicate(x) ? operation(x) : x;
      content.innerHTML = "The original value of x is " + x + " and the result is " + result + ".<br>";
      let result2 = predicate(3) ? operation(3) : 3;
      content.innerHTML += "The original value of x is " + 3 + " and the result is " + result2 + ".<br>";
   </script>
</body>
</html>

Example

We have created the various predicate and operation functions in the example below. The operation1() function returns the value after dividing by 2. The operation2() function returns the value by converting negative to positive.

Using the modulo operator, the predicate1() function checks whether the x is divisible by 2. The predicate2() function checks whether the x is less than 0.

We can pass the predicate and operation function as a parameter of the test function and execute them inside the test function. In such a way, we can test the value of x against different predicate functions and perform different tasks on x using different operation functions.

<html>
<body> 
   <h3>Testing a value x <i> against predicate function and returns fn(x) or x </i> in JavaScript</h3>
   <div id = "content"> </div>
   <script>
      let content = document.getElementById("content");
      function operation1(x) {
         return x / 2;
      }
      function predicate1(x) {
         return x % 2 == 0;
      }
      function operation2(x) {
         return Math.abs(x);
      }
      function predicate2(x) {
         return x < 0;
      }
      function test(x, predicate, operation) {
         content.innerHTML += "The original value of x is " + x + "<br>";
         let res = predicate(x) ? operation(x) : x;
         content.innerHTML += "The value of x after the test is " + res + "<br>";
      }
      test(6, predicate1, operation1);
      test(-6, predicate2, operation2);
   </script>
</body> 
</html>

Users learned to test the value of x against the predicate function and return the fn(x) or x based on the returned value of the predicate function. All things users need to do is use the ternary operator. Use the predicate function in the condition part. If the condition becomes true, return the calculated value from the operation function; otherwise, return the value of x itself.

Updated on: 09-Mar-2023

60 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements