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 function scaling with its arguments, where the function scaling transforms the given arguments by scaling the arguments by scaling its arguments to once, twice, and thrice.
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!
Syntax:
1st way :
function function_name ( argument1, argument2,....){
instruction1;
instruction2;
....
return parameters;
}2nd way:
var function_name = function ( argument1, argument2,....){
instruction1;
instruction2;
....
return parameters;
}Example:
Javascript
<script> function scaling(num1, num2, num3) { return [1 * num1, 2 * num2, 3 * num3]; } function transformer(num1, num2, num3) { var tran=scaling(num1, num2, num3); console.log(tran); } var num1=5; var num2=5; var num3=5; transformer(num1, num2, num3);</script> |
Output: Below the output is the array of transformed arguments.
[5, 10, 15]
Example:Transforming the arguments to their squares by invoking the function squares.
Javascript
<script> function square(num1, num2, num3) { return [num1 * num1, num2 * num2, num3 * num3]; } function transformer(num1, num2, num3) { var tran = square(num1, num2, num3); console.log(tran); } var num1 = 5; var num2 = 10; var num3 = 15; transformer(num1, num2, num3);</script> |
Output:
[25, 100, 225]

