I have a function that divides two input arguments:
const divide = (x, y) => {
return x / y;
};
I have a second function that takes the divide function as its input argument and returns a new function.
function test(func) {
return function(){
return func();
}
}
const retFunction = test(divide);
retFunction(24, 3)
I am expecting the returned value to be 8 (24 / 3). But I'm getting a returned output of 'NaN'. What am I doing wrong?
Division (/)The division operator ( / ) produces the quotient of its operands where the left operand is the dividend and the right operand is the divisor.
Function arguments are the real values passed to (and received by) the function.
A JavaScript function is defined with the function keyword, followed by a name, followed by parentheses ().
You need to pass the possible arguments to the function: ...args
:
const divide = (x, y) => {
return x / y;
};
function test(func) {
return function(...args) {
return func(...args);
}
}
const retFunction = test(divide);
const result = retFunction(24, 3);
console.log(result);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With