Jump to content

Functions as Arguments


Recommended Posts

Fernando_Diniz

Start by repeating the code you wrote for the last section (the calculate and add functions).

Declare a variable multiply and assign to it a function declaration taking two arguments, operand1 and operand2, and which returns the result of the multiplication of the arguments.

Invoke calculate twice, passing it as a third argument the add and multiply functions respectively.

 

Alguém para ajudar ??

var add = function (operand1, operand2){
  return operand1 + operand2;
};
var calculate = function (operand1, operand2, operation) { 
  var result = operation(operand1, operand2);
console.log(result);  
};
var multiply = function (operand1, operand2) {
    return operand1*operand2;
}; 
calculate=(4,4,add);
calculate=(4,4,multiply);

 

Link to post
Share on other sites
Zex

var add = function (operand1, operand2){
  return operand1 + operand2;
};

var multiply = function (operand1, operand2) {
    return operand1*operand2;
}; 


var calculate = function (operand1, operand2, operation ) 
{ 
  var result = operation(operand1, operand2);
  return( result );  
};

console.log( calculate(4,4,add) );
console.log( calculate(4,4,multiply) );

 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.