How to create a function that invokes each provided function with the arguments it receives using JavaScript ?
Last Updated :
02 Jan, 2023
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
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 the FindMinMax function and are invoked in the 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"
Similar Reads
How to create a function that invokes the provided function with its arguments transformed in JavaScript ? In programming, functions are used to reduce the effort of writing the same instance of code repeatedly. In this article let us see how we can create a function that invokes the provided function with its arguments transformed in JavaScript. In this article, the function transformer invokes the func
2 min read
How to create a function that invokes function with partials prepended arguments in JavaScript ? In this article, we will see how to create a function that invokes functions with partials prepended to the arguments it receives in JavaScript. Before understanding the problem statement and approaching the solution for the same, let's, first of all, know what a function (also called a method) is a
5 min read
How to create a function that invokes function with partials appended to the arguments in JavaScript ? In this article, we will learn to create a function that invokes a function with partials appended to the arguments it receives in JavaScript. Approach: We want to implement a function that invokes another function and provides the arguments it received. We can get the result by using (...) the spre
3 min read
How to implement a function that enable another function after specified time using JavaScript ? Let us assume that we have been given a function add() which takes in two parameters a and b, and returns their sum. We are supposed to implement a function that should be able to call any function after the given delay amount of time. This can be done with the approaches as given below: Approach 1:
2 min read
How to get the function name from within that function using JavaScript ? Given a function and the task is to get the name of the function from inside the function using JavaScript. There are basically two methods to get the function name from within that function in JavaScript. These are: Using String substr() MethodUsing Function prototype name propertyGet the Function
2 min read
How to create a function that invokes the method at a given key of an object in JavaScript ? In JavaScript objects store variables in the key-value pair where the key is the property of the object and the value maybe the value of the property, or it may be any method that invokes when it is called. In this article, we will learn to invoke a method that is provided to key. Approach: Generall
2 min read