Skip to content
Related Articles

Related Articles

Improve Article

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

  • Difficulty Level : Medium
  • Last Updated : 26 May, 2021
Geek Week

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

Example1: Function to seperate even and odd numbers in an array. The findEven and findOdd functions take the same arguments by segregateEvenodd function and are invoked in segregateEvenodd function.

Javascript




<script>
  // Defined array
  var ar = 
      [1, 2, 2, 3, 4, 5, 6, 6, 7, 8, 8, 8];
  function findEven(ar){
    var res1 = [];
    for (let geek = 0; geek < ar.length; geek++) {
        if (ar[geek] % 2 === 0) {
            res1.push(ar[geek]);
        }
    }
    return res1;
  }
  
  function findOdd(ar){
    var res2 = [];
    for (let geek = 0; geek < ar.length; geek++) {
        if (ar[geek] % 2 === 1) {
            res2.push(ar[geek]);
        }
    }
    return res2;
  }
  
  function segregateEvenOdd(ar) {
  
    // Invoking findEven and findOdd functions
    var even = findEven(ar);
    var odd = 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 FindMinMax function and are invoked in FindMinMax function.

Javascript




<script>
  // Defined array
  var ar = [20, 30, 40, 50, 60, -20, -40, 90, 100];
  function findMin(ar) {
      var res1 = Number.MAX_VALUE;;
      for (let geek = 0; geek < ar.length; geek++) {
          if (ar[geek] < res1) {
              res1 = ar[geek];
          }
      }
      return res1;
  }
  function findMax(ar) {
      var res2 = Number.MIN_VALUE;
      for (let geek = 0; geek < ar.length; geek++) {
          if (ar[geek] > res2) {
              res2 = ar[geek];
          }
      }
      return res2;
  }
  
  function FindMinMax(ar) {
      // Invoking findMin and findMax functions
      var min = findMin(ar);
      var max = 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"

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!




My Personal Notes arrow_drop_up
Recommended Articles
Page :