We help IT Professionals succeed at work.

# callback function

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){
} else if( callback == "multiply"){
multiply();
}
}

``````

//WHY IS MY CALLBACK NOT WORKING?
Comment
Watch Question

## View Solution Only

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){
} else if( callback == "multiply"){
return multiply(num1, num2);
}
}
``````
Student

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!
Database 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)
{
}
else if(callback == "multiply")
{
return multiply(num1, num2);
}
}

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

OUTPUT

10

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

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

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

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

OUTPUT
7
Database Expert
Awarded 2016
Top Expert 2016

Commented:
Please try this. Why to create extra functions.

``````function math(num1, num2, callback)
{

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

var vals = math(2, 5, 'add');
document.write(vals);
``````
Most Valuable Expert 2017
Distinguished Expert 2019
Commented:
You can also pass the function itself in other words
``````<script>
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>
``````
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
Student

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

Commented:
You are welcome.