Skip to content

Commit

Permalink
Update Object.js
Browse files Browse the repository at this point in the history
  • Loading branch information
perk-bevis authored Dec 25, 2024
1 parent 999fb8a commit aa8c399
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions Object.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,25 @@ console.log(calculate('multiply', 5, 3));
console.log(calculate('divide', 9, 3));
console.log(calculate('divide', 5, 0));
console.log(calculate modulus, 3, 31);
🔴🔴🔴🔴🔴🔴
function calculate (operation, a, b) {
const calculator = {
add: () => a + b,
subtract: () => ab,
multiply: () => a * b,
divide: () => b === 0 ? 'Cannot divide by zero.': a / b
};
const action = calculator [operation];
if (action) {
return action();
}
return 'Operation not recognized. Please use add, subtract,multiply, or divide.';
}

// Testing the function
console.log(calculate('add', 5, 3));
console.log(calculate('subtract', 5, 5));
console.log(calculate('multiply', 5, 3));
console.log(calculate('divide', 9, 3));
console.log(calculate('divide', 5,0));
console.log(calculate('modulus', 5, 3));

0 comments on commit aa8c399

Please sign in to comment.