How to implement a simple calculator using node js

// Function to perform basic arithmetic operations
function calculator(num1, operator, num2) {
  switch (operator) {
    case '+':
      return num1 + num2;
    case '-':
      return num1 - num2;
    case '*':
      return num1 * num2;
    case '/':
      return num1 / num2;
    default:
      return 'Invalid operator';
  }
}

// Example usage
const resultAddition = calculator(5, '+', 3);
console.log(resultAddition);

 

Post your Answer