callback function

/*
Instructions

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.

*/

function add(num1, num2){
  return num1 + num2;
}

function multiply(num1, num2){
  return num1 * num2;
}

function math(num1, num2, callback){
  if(callback == "add"){
     add();
  } else if( callback == "multiply"){
     multiply();
  }
}

console.log(math(2, 5, 'add'))

Open in new window




//WHY IS MY CALLBACK NOT WORKING?
Temir AlievStudentAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongCommented:
is this a homework?

you can write it like below in which you need to pass the parameters and then return the value:

function math(num1, num2, callback){
  if(callback == "add"){
      return add(num1, num2);
  } else if( callback == "multiply"){
      return multiply(num1, num2);
  }
}

Open in new window

0
Temir AlievStudentAuthor Commented:
Yes, it is a homework, I tried what you wrote, but this teaching platform isn't accepting the answer, although it works on repl.it outside my school platform. I guess it is just a bug that they need to fix. Thanks for your time, Ryan, I really appreciate it!
0
Pawan KumarDatabase ExpertCommented:
Your school system is looking very old. :) Below is working fine for me.

function add(num1, num2){
  return num1 + num2;
}

function multiply(num1, num2){
  return num1 * num2;
}

function math(num1, num2, callback)
{
  if(callback == "add"){
     return add(num1, num2);
  } 
  else if(callback == "multiply")
  {
     return multiply(num1, num2);
  }
}

var vals = math(2, 5, "multiply");
document.write(vals);

Open in new window


OUTPUT

10

function add(num1, num2){
  return num1 + num2;
}

function multiply(num1, num2){
  return num1 * num2;
}

function math(num1, num2, callback)
{
  if(callback == "add"){
     return add(num1, num2);
  } 
  else if(callback == "multiply")
  {
     return multiply(num1, num2);
  }
}

var vals = math(2, 5, "add");
document.write(vals);

Open in new window


OUTPUT
7
1
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Pawan KumarDatabase ExpertCommented:
Please try this. Why to create extra functions.

function math(num1, num2, callback)
{
  
  if(callback == "add"){
    return(num1+ num2);
  } 
  else if(callback == "multiply")
  {
    return(num1*num2);
  }
}

var vals = math(2, 5, 'add');
document.write(vals);

Open in new window

1
Julian HansenCommented:
You can also pass the function itself in other words
<script>
function add(num1, num2){
  return num1 + num2;
}

function multiply(num1, num2){
  return num1 * num2;
}

function math(num1, num2, callback){
  return callback(num1, num2);
}
console.log(math(2, 5, multiply))
</script>

Open in new window

In your original code you are using a string value to determine which function to call - you were (as Ryan pointed out) not returning a value from the math function - so the code was "working" it just wasn't doing anything visible.

The above solution uses the principle of passing a function as a parameter - this is the typical definition of a callback. A callback is a piece of code that is invoked from another piece of code based on certain criteria being met - it is typically used when you want to customise what happens after a particular event or result is reached.

Your question does not make it clear which version you should use -but as this is a teaching example I am going to guess they are trying to teach you what a callback is so I would look at using the above code
2

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Temir AlievStudentAuthor Commented:
Thank you Julian, your explanations are great as always!
0
Julian HansenCommented:
You are welcome.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.