Wednesday, July 3, 2024
HomeLanguagesJavascriptHow to create a function that invokes the provided function with its...

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.

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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

Shaida Kate Naidoo
am passionate about learning the latest technologies available to developers in either a Front End or Back End capacity. I enjoy creating applications that are well designed and responsive, in addition to being user friendly. I thrive in fast paced environments. With a diverse educational and work experience background, I excel at collaborating with teams both local and international. A versatile developer with interests in Software Development and Software Engineering. I consider myself to be adaptable and a self motivated learner. I am interested in new programming technologies, and continuous self improvement.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments