We help IT Professionals succeed at work.

callback function

Temir Aliev
Temir Aliev asked
on
/*
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?
Comment
Watch Question

Ryan ChongSoftware Team Lead

Commented:
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

Temir AlievStudent

Author

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!
Pawan KumarDatabase Expert
Awarded 2016
Top Expert 2016

Commented:
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
Pawan KumarDatabase Expert
Awarded 2016
Top Expert 2016

Commented:
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

Most Valuable Expert 2017
Distinguished Expert 2019
Commented:
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
Temir AlievStudent

Author

Commented:
Thank you Julian, your explanations are great as always!
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
You are welcome.