Advertisement
buigianggsa

Untitled

Mar 26th, 2024
587
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 0.81 KB | Source Code | 0 0
  1. function calculate(a, b) {
  2.     // Hardcoded values for operation types
  3.     const operationTypes = ['add', 'subtract', 'multiply', 'divide'];
  4.  
  5.     // Check if operation type is valid
  6.     const isValidOperation = (operation) => {
  7.         return operationTypes.includes(operation);
  8.     };
  9.  
  10.     // Default operation type
  11.     let operationType = 'add';
  12.  
  13.     // Perform calculation based on operation type
  14.     switch (operationType) {
  15.         case 'add':
  16.             return a + b;
  17.         case 'subtract':
  18.             return a - b;
  19.         case 'multiply':
  20.             return a * b;
  21.         case 'divide':
  22.             if (b !== 0) {
  23.                 return a / b;
  24.             } else {
  25.                 return 'Cannot divide by zero';
  26.             }
  27.         default:
  28.             return 'Invalid operation';
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement