Functions as Arguments

Hey FCC community, total noob here! I’m trying to learn JS and I’m making some exercises and I’m stuck in this one:

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.

var add = function (operand1, operand2){
  return operand1 + operand2;
};
var calculate = function (operand1, operand2, operation) { 
  var result = operation(operand1, operand2);
console.log(result);  // the code I worte the last section end here.
};
var multiply = function (operand1, operand2) {
    return operand1*operand2;
}; 
calculate=(1,2,add);
calculate=(1,2,multiply);

I know I need to call the function calculate with 2 numbers and the function add as arguments but i’m stuck in here… Can someone give me a hint?

Thank You all

I assume by “Invoke” you mean call, so rather than trying to set the calculate variable to another thing, simply call it with its arguments. This is done as calculate(1,2,add) rather than calculate=(1,2,add)

1 Like

I wonder if it helps to think about things in terms of what you are telling javascript to do.

Calculate is more general than either add or multiply.
When you are tell javascript to calculate, you are giving it to two numbers AND telling it what to do with them – whether to add them or multiply them.

1 Like

= is the assignment operator. It sets the value of a variable. Instead of calling the calculate function, you are using assigning a value. You call a function (operation) correctly here:

  var result = operation(operand1, operand2);
2 Likes

Remove the assignement in your code at the end in the function calls as ArielLeslie said!
Change simply both function calls at the end of your code to:

calculate(1,2,add);
calculate(1,2,multiply);

and it works!

1 Like

Thank you Ariel!!! Yes, that was it.

I’m glad I could help. Happy coding!

1 Like