Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using callbacks to add and multiply two numbers

I was given a prompt to complete and this is what is says

Write two functions, one called add and one called multiply, that each takes in two numbers and returns the appropriate new value.

Write a function called math that takes in two numbers, and a function 'operator' as parameters.

  • This function should return a callback invoked with the appropriate arguments.

I have almost completed the problem and am stuck as to what to do to finish, any help would be appreciated. This is what I have.

function add(num1, num2){
  return num1 + num2;
}
function multiply(num1, num2){
  return num1 * num2;
}
function math(num1, num2, func){
  return func();
}
math(1,2,add);

the log is only returning nan and i am not sure why it is not a number, I am also not sure if the code is written how they want with callbacks?

like image 622
Devin Bowen Avatar asked Mar 08 '23 11:03

Devin Bowen


1 Answers

You should change math function to use num1 and num2 when you call func

function math(num1, num2, func){
  return func(num1, num2);
}
like image 145
Pratansyah Avatar answered Mar 16 '23 02:03

Pratansyah