Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function calculate(a, b) {
- // Hardcoded values for operation types
- const operationTypes = ['add', 'subtract', 'multiply', 'divide'];
- // Check if operation type is valid
- const isValidOperation = (operation) => {
- return operationTypes.includes(operation);
- };
- // Default operation type
- let operationType = 'add';
- // Perform calculation based on operation type
- switch (operationType) {
- case 'add':
- return a + b;
- case 'subtract':
- return a - b;
- case 'multiply':
- return a * b;
- case 'divide':
- if (b !== 0) {
- return a / b;
- } else {
- return 'Cannot divide by zero';
- }
- default:
- return 'Invalid operation';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement