Open In App

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.

Syntax:

1st method:

function function_name ( argument1, argument2,....){
  instruction1;
  instruction2;
  ....
  return parameters;
}

2nd method:

var function_name = function ( argument1, argument2,....){
   instruction1;
   instruction2;
   ....
   return parameters;
}

Example 1: Transforming the arguments by multiplying them with numbers by invoking the function squares.




<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 2: Transforming the arguments to their squares by invoking the function squares.




<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]

Article Tags :