Advertisement
TZinovieva

Math Operations JS Advanced

Sep 14th, 2023 (edited)
537
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function mathOperations(num1, num2, sign) {
  2.     const operations = {
  3.         '+': (a, b) => a + b,
  4.         '-': (a, b) => a - b,
  5.         '*': (a, b) => a * b,
  6.         '/': (a, b) => a / b,
  7.         '%': (a, b) => a % b,
  8.         '**': (a, b) => a ** b
  9.     };
  10.  
  11.     const result = operations[sign](num1, num2);
  12.     console.log(result);
  13. }
  14.  
  15. OR
  16.  
  17. function mathOperations(num1, num2, sign) {
  18.     let result = 0;
  19.     if (sign === '+') {
  20.         result = num1 + num2;
  21.     } else if (sign === '-') {
  22.         result = num1 - num2;
  23.     } else if (sign === '*') {
  24.         result = num1 * num2;
  25.     } else if (sign === '/') {
  26.         result = num1 / num2;
  27.     } else if (sign === '%') {
  28.         result = num1 % num2;
  29.     } else if (sign === '**') {
  30.         result = Math.pow(num1, num2);
  31.     }
  32.     console.log(result);
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement