-
Notifications
You must be signed in to change notification settings - Fork 0
/
Calculator.js
124 lines (113 loc) · 3 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/**
* @author WorldsDumbestPerson224
* @copyright 2024
* @readonly true
**/
class Calculator{
add(a, b){
/**
* @description This method adds two numbers
* @param {number} a
* @param {number} b
* @returns {number} a - b;
**/
return a + b;
}
subtract(a, b){
/**
* @description This method subtracts two numbers
* @param {number} a
* @param {number} b
* @returns {number} a - b;
**/
return a - b;
}
multiply(a, b){
/**
* @description This method multiplies two numbers
* @param {number} a
* @param {number} b
* @returns {number} a * b
**/
return a * b;
}
divide(a, b){
/**
* @description This method divides two numbers
* @param {number} a
* @param {number} b
* @throws {string} "Division by zero error"
* @returns a / b
**/
try{
if(a != 0){
return a / b;
}else{
throw "Division by zero error";
}
}catch(error){
console.error(error);
}
}
round_up(a){
/**
* @param {number} a
* @returns {number} a rounded up
**/
return Math.ceil(a);
}
round_down(a){
/**
* @param {number} a
* @returns {number} a rounded down
**/
return Math.floor(a);
}
}
// Create an instance of Calculator
var calculator = new Calculator();
function get_input(operation, num1, num2){
/**
* @description Get the parameters and call DoOperation with the parameters
* @calls DoOperation
* @param {string} operation
* @param {number} num1
* @param {number} num2
**/
if(typeof num1 == "number" && typeof num2 == "number" && typeof operation == "string"){
doOperation(operation, num1, num2);
}else{
console.error("Return invalid types!");
}
}
function doOperation(operation, num1, num2){
/**
* @description This does the operation provided from the get_input function
* @param {string} operation
* @param {number} num1
* @param {number} num2
* @returns {number} the result of the operation
**/
switch(operation){
case "+":
calculator.add(num1, num2);
break;
case "-":
calculator.subtract(num1, num2);
break;
case "*":
calculator.multiply(num1, num2);
break;
case "/":
calculator.divide(num1, num2);
break;
case "roundup":
calculator.round_up(num1);
break;
case "rounddown":
calculator.round_down(num1);
break;
default:
console.error("No operand provided!");
}
}