-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.js
68 lines (58 loc) · 1.66 KB
/
calculator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
const OPERATIONS = ["+", "-", "*", "/", undefined];
const NUMBER_LIMIT = 1000;
function validateType(array, type) {
array.forEach((element) => {
if (typeof element !== type) throw new Error("Invalid input type");
});
}
function validateOperators(array) {
array.forEach((element) => {
if (!OPERATIONS.includes(element)) throw new Error("Invalid operator");
});
}
function applyOperation(num1, num2, operation) {
switch (operation) {
case "+":
return num1 + num2;
case "-":
return num1 - num2;
case "*":
return num1 * num2;
case "/":
if (num2 === 0) throw new Error("Division by zero");
return num1 / num2;
default:
throw new Error("Unknown operation");
}
}
const precedence = (op) => {
if (op === "+" || op === "-") return 1;
if (op === "*" || op === "/") return 2;
};
function calculator(calculatorInputs) {
var {
num1,
operation1,
num2,
operation2 = undefined,
num3 = 0,
} = calculatorInputs;
validateType([num1, num2, num3], "number");
validateOperators([operation1, operation2]);
// Task Prerequsitie: ignore numbers bigger than NUMBER_LIMIT
if (num1 > NUMBER_LIMIT) num1 = 0;
if (num2 > NUMBER_LIMIT) num2 = 0;
if (num3 > NUMBER_LIMIT) num3 = 0;
if (!operation2) return applyOperation(num1, num2, operation1);
else {
let total;
if (precedence(operation1) > precedence(operation2)) {
total = applyOperation(num1, num2, operation1);
return applyOperation(total, num3, operation2);
} else {
total = applyOperation(num2, num3, operation2);
return applyOperation(num1, total, operation1);
}
}
}
module.exports = calculator;