最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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);
}
发布评论

评论列表(0)

  1. 暂无评论