forked from dshaplyko/js-mentoring-program
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.js
44 lines (38 loc) · 992 Bytes
/
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
class Claculator {
constructor(){
}
static checkIfArgsAreNumbers(a, b) {
const firstArgType = typeof a;
const secondArgType = typeof b;
if (firstArgType === 'number' && secondArgType === 'number') {
return;
}
throw Error(`Both arguments are not of type number! First argument is [${firstArgType}, second argument is [${secondArgType}]]!`);
}
static add(a, b) {
this.checkIfArgsAreNumbers(a, b);
return a + b;
};
static subtract(a, b) {
this.checkIfArgsAreNumbers(a, b);
return a - b;
}
static multiply(a, b) {
this.checkIfArgsAreNumbers(a, b);
return a * b;
}
static divide(a, b) {
this.checkIfArgsAreNumbers(a, b);
return a / b;
}
static factorial(n) {
const typeOfArg = typeof n;
if (typeOfArg === 'number') {
if (n === 0) {
return 1;
}
return n * this.factorial (n - 1);
}
throw Error(`Argument is not of type number! It is of type [${typeOfArg}]!`);
}
}