Functions as Arguments sera que alguem aqui entende sobre isso

2 respostas Resolvido
javascript
Z

Declare the variable add and assign it a function which takes two arguments, operand1 and operand2, and returns the sum of those arguments.

Declare the variable calculate and assign it a function which takes three arguments, operand1, operand2 and operation.

Inside the body of calculate, invoke the function passed as the argument operation, passing as arguments operand1 and operand2, and print the result of this invocation to the console.

Dont forget to invoke calculate passing it the add function as a third argument.



var add = function (operand1, operand2){

return operand1 + operand2;

};

var calculate = function (operand1, operand2, add) {

return operand2 + add;

};
Output

Code is incorrect
Create a variable in the body of the calculate function to invoke the function received as argument

2 Respostas

D

Não seria…

let sum = add;
return operand2 + sum;

?

A
Solucao aceita

Parece que você está quase lá, só faltou invocar a função no terceiro passo

var add = function (operand1, operand2){
  return operand1 + operand2;
};
var calculate = function (operand1, operand2, operation) { 
// mais tarde
}
var calculate = function (operand1, operand2, operation) { 
  var result = operation(operand1, operand2);  // isso aqui é invocar a função
  console.print(result)
}
calculate(1,2, add); // tem que aparecer 3 no console
Criado 15 de fevereiro de 2020
Ultima resposta 17 de fev. de 2020
Respostas 2
Participantes 3