为什么结果是undefined?求解


function calculate(command,a,b) {
  if (command === 'add') {
    add(a,b);
  } else if (command === 'substract') {
    substract(a,b);
  } else if (command === 'divide') {
    divide(a,b);
  } else if (command === 'remainder') {
    remainder(a,b);
  }
}
const add = function (a,b) {
  return a + b;
}
const substract = (a,b) => a - b;
const divide = (a,b) => a / b;
const remainder = function (a,b) {
  return a % b;
}
console.log(calculate('add',1,2));

你 calculate(command,a,b) 函数没有用 return 返回add(a,b)的值。
应该是

function calculate(command,a,b) {
  if (command === 'add') {
    return add(a,b);
  } else if (command === 'substract') {
    return substract(a,b);
  } else if (command === 'divide') {
    return divide(a,b);
  } else if (command === 'remainder') {
    return remainder(a,b);
  }
}
const add = function (a,b) {
  return a + b;
}
const substract = (a,b) => a - b;
const divide = (a,b) => a / b;
const remainder = function (a,b) {
  return a % b;
}
console.log(calculate('add',1,2));