-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcalculator.js
More file actions
54 lines (46 loc) · 1.19 KB
/
calculator.js
File metadata and controls
54 lines (46 loc) · 1.19 KB
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
// A simple calculator with some issues
class Calculator {
constructor() {
this.result = 0;
}
// Add numbers without validation
add(a, b) {
return a + b;
}
// Subtract numbers without validation
subtract(a, b) {
return a - b;
}
// Multiply numbers without validation
multiply(a, b) {
return a * b;
}
// Divide numbers without any error handling
divide(a, b) {
if (b === 0) throw new Error("Division by zero is not allowed");
return a / b;
}
// Power calculation that can be slow for large exponents
power(base, exponent) {
let result = 1;
for (let i = 0; i < exponent; i++) {
result *= base;
}
return result;
}
// No validation for factorial of negative numbers
factorial(n) {
if (n === 0) return 1;
return n * this.factorial(n - 1);
}
// Poor variable naming and lacks clarity
calc(x, y, z) {
if (z == '+') return this.add(x, y);
if (z == '-') return this.subtract(x, y);
if (z == '*') return this.multiply(x, y);
if (z == '/') return this.divide(x, y);
if (z == '^') return this.power(x, y);
if (z == '!') return this.factorial(x);
}
}
module.exports = Calculator;