javascript - using callbacks to add and multiply two numbers - Stack Overflow

I was given a prompt to plete and this is what is saysWrite two functions, one called add and one calle

I was given a prompt to plete 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 pleted 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);

I was given a prompt to plete 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 pleted 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?

Share Improve this question edited Nov 27, 2017 at 5:05 Salomon Zhang 1,5653 gold badges23 silver badges42 bronze badges asked Nov 27, 2017 at 2:09 Devin BowenDevin Bowen 892 silver badges9 bronze badges 1
  • 1 Your function math(num1, num2, func){ return func(); } return function not gives any params – Salomon Zhang Commented Nov 27, 2017 at 2:24
Add a ment  | 

1 Answer 1

Reset to default 7

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

function math(num1, num2, func){
  return func(num1, num2);
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744961519a4603414.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信