-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
125 lines (105 loc) · 3.74 KB
/
script.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
125
const keys = document.querySelectorAll('.key');
const display_input = document.querySelector('.display .input');
const display_output = document.querySelector('.display .output');
let input = "";
for (let key of keys) {
const value = key.dataset.key;
key.addEventListener('click', () => {
if (value == "clear") {
input = "";
display_input.innerHTML = "";
display_output.innerHTML = "";
} else if (value == "backspace") {
input = input.slice(0, -1);
display_input.innerHTML = CleanInput(input);
} else if (value == "=") {
let result = eval(PerpareInput(input));
display_output.innerHTML = CleanOutput(result);
} else if (value == "brackets") {
if (
input.indexOf("(") == -1 ||
input.indexOf("(") != -1 &&
input.indexOf(")") != -1 &&
input.lastIndexOf("(") < input.lastIndexOf(")")
) {
input += "(";
} else if (
input.indexOf("(") != -1 &&
input.indexOf(")") == -1 ||
input.indexOf("(") != -1 &&
input.indexOf(")") != -1 &&
input.lastIndexOf("(") > input.lastIndexOf(")")
) {
input += ")";
}
display_input.innerHTML = CleanInput(input);
} else {
if (ValidateInput(value)) {
input += value;
display_input.innerHTML = CleanInput(input);
}
}
})
}
function CleanInput(input) {
let input_array = input.split("");
let input_array_length = input_array.length;
for (let i = 0; i < input_array_length; i++) {
if (input_array[i] == "*") {
input_array[i] = ` <span class="operator">x</span> `;
} else if (input_array[i] == "/") {
input_array[i] = ` <span class="operator">÷</span> `;
} else if (input_array[i] == "+") {
input_array[i] = ` <span class="operator">+</span> `;
} else if (input_array[i] == "-") {
input_array[i] = ` <span class="operator">-</span> `;
} else if (input_array[i] == "(") {
input_array[i] = `<span class="brackets">(</span>`;
} else if (input_array[i] == ")") {
input_array[i] = `<span class="brackets">)</span>`;
} else if (input_array[i] == "%") {
input_array[i] = `<span class="percent">%</span>`;
}
}
return input_array.join("");
}
function CleanOutput(output) {
let output_string = output.toString();
let decimal = output_string.split(".")[1];
output_string = output_string.split(".")[0];
let output_array = output_string.split("");
if (output_array.length > 3) {
for (let i = output_array.length - 3; i > 0; i -= 3) {
output_array.splice(i, 0, ",");
}
}
if (decimal) {
output_array.push(".");
output_array.push(decimal);
}
return output_array.join("");
}
function ValidateInput(value) {
let last_input = input.slice(-1);
let operators = ["+", "-", "*", "/"];
if (value == "." && last_input == ".") {
return false;
}
if (operators.includes(value)) {
if (operators.includes(last_input)) {
return false;
} else {
return true;
}
}
return true;
}
function PerpareInput(input) {
let input_array = input.split("");
for (let i = 0; i < input_array.length; i++) {
if (input_array[i] == "%") {
input_array[i] = "/100";
}
}
return input_array.join("");
}