• API
• FAQ
• Tools
• Archive
SHARE
TWEET # Untitled a guest Dec 6th, 2018 79 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. function operationsWithNumbers([arg1, arg2, arg3]) {
2.     let n1 = Number(arg1);
3.     let n2 = Number(arg2);
4.     let operator = arg3;
5.
6.     let result = 0;
7.
8.     switch (operator) {
9.         case ("+"): result = n1 + n2;
10.             break;
11.         case ("-"): result = n1 - n2;
12.             break;
13.         case ("*"): result = n1 * n2;
14.             break;
15.         case ("/"): result = n1 / n2;
16.             break;
17.         case ("%"): result = n1 % n2;
18.             break;
19.     }
20.
21.     let evenOrOdd = "";
22.     if ((operator == "+" || operator == "-" || operator == "*") && result % 2 == 0) {
23.         evenOrOdd = " - even";
24.     } else if ((operator == "+" || operator == "-" || operator == "*") && result % 2 == 1) {
25.         evenOrOdd = " - odd";
26.     }
27.
28.     if (operator == "/") {
29.         result = result.toFixed(2);
30.     }
31.
32.     if (n2 == 0 && (operator == "/" || operator == "%")) {
33.         result = `Cannot divide \${n1} by zero`;
34.     }
35.
36.     if (operator == "+" || operator == "-" || operator == "*") {
37.         console.log(`\${n1} \${operator} \${n2} = \${result}\${evenOrOdd}`);
38.     } else if (n2 == 0 && (operator == "/" || operator == "%")) {
39.         console.log(result);
40.     } else {
41.         console.log(`\${n1} \${operator} \${n2} = \${result}`);
42.     }
43. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.
Not a member of Pastebin yet?