How to create a function that invokes each provided function with the arguments it receives using JavaScript ?

In this article, we will see how to create a function that invokes each provided function with the arguments it receives using JavaScript.
It helps in reducing the effort of creating the same instance of code again and again. In this article let us see how to create a function that invokes each provided function with the arguments it receives and returns a value in JavaScript.
Functions can be defined in two ways:
Syntax:
function <function_name>(arg_1,arg_2,...)
{
   <set of instructions>;
}
//INVOKER
var returned_value= function_name(arguments);
ORÂ
var function_name = function(arg_1,arg_2,...)
{
  <set of instructions>;
}
// INVOKER
var returned_value = function_name(arguments);
Below are the examples that help us to understand the problem easily.
Example 1: Function to separate even and odd numbers in an array. The findEven and findOdd functions take the same arguments by the segregateEvenodd function and are invoked in the segregateEvenodd function.
Javascript
| <script>     // Defined array     varar =          [1, 2, 2, 3, 4, 5, 6, 6, 7, 8, 8, 8];     functionfindEven(ar){       varres1 = [];       for(let geek = 0; geek < ar.length; geek++) {           if(ar[geek] % 2 === 0) {               res1.push(ar[geek]);           }       }       returnres1;     }      Â    functionfindOdd(ar){       varres2 = [];       for(let geek = 0; geek < ar.length; geek++) {           if(ar[geek] % 2 === 1) {               res2.push(ar[geek]);           }       }       returnres2;     }      Â    functionsegregateEvenOdd(ar) {      Â      // Invoking findEven and findOdd functions       vareven = findEven(ar);       varodd = findOdd(ar);       console.log("Before Segregation: ");       console.log(ar);       console.log("After Segregation: ");       console.log("Even integers: "+ even);       console.log("Odd integers: "+ odd);     }      Â    // Invoker     segregateEvenOdd(ar); </script> | 
Output:
"Before Segregation: " [1, 2, 2, 3, 4, 5, 6, 6, 7, 8, 8, 8] "After Segregation: " "Even integers: 2,2,4,6,6,8,8,8" "Odd integers: 1,3,5,7"
Example 2: Function to find minimum and maximum in an array. The findMin and findMax functions take the same arguments by the FindMinMax function and are invoked in the FindMinMax function.
Javascript
| <script>     // Defined array     varar = [20, 30, 40, 50, 60, -20, -40, 90, 100];     functionfindMin(ar) {         varres1 = Number.MAX_VALUE;;         for(let geek = 0; geek < ar.length; geek++) {             if(ar[geek] < res1) {                 res1 = ar[geek];             }         }         returnres1;     }     functionfindMax(ar) {         varres2 = Number.MIN_VALUE;         for(let geek = 0; geek < ar.length; geek++) {             if(ar[geek] > res2) {                 res2 = ar[geek];             }         }         returnres2;     }      Â    functionFindMinMax(ar) {         // Invoking findMin and findMax functions         varmin = findMin(ar);         varmax = findMax(ar);         console.log("Given array : ");         console.log(ar);         console.log("Minimum in the array: "+ min);         console.log("Maximum in the array: "+ max);      Â    }     // Invoker     FindMinMax(ar); </script> | 
Output:
"Given array : " [20, 30, 40, 50, 60, -20, -40, 90, 100] "Minimum in the array: -40" "Maximum in the array: 100"
 
				 
					


