Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
204 lines (160 loc) · 6.53 KB
/
Copy pathscript.js
File metadata and controls
204 lines (160 loc) · 6.53 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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/******************************CONSTANTS*************************************/
const ERROR_DIVIDE_BY_ZERO = 'Division by Zero does not work here.';
const ERROR_NO_VALID_OPERATOR = 'Not a valid Operator.';
const mainResult = document.getElementById('mainResult');
const upperResult = document.getElementById('upperResult');
const numberButtons = Array.from(document.getElementsByClassName('number'));
const operatorButtons = Array.from(document.getElementsByClassName('operator'));
const clearButton = document.getElementById('clearButton');
const deleteButton = document.getElementById('deleteButton');
const resultButton = document.getElementById('resultButton');
const commaButton = document.getElementById('commaButton');
const infoBox = document.getElementById('infobox');
/******************************GLOBALS***************************************/
let first = '';
let second = '';
let currentOperator = '';
let res = '';
/******************************EVENTSLISTENERS*******************************/
numberButtons.forEach(numberButton => {
numberButton.addEventListener("click", () => {
if(first == '' && second == ''){
first = appendNumber(numberButton.textContent);
updateMainResultScreen(first);
} else if (first != '' && second == '' && currentOperator == ''){
first = appendNumber(numberButton.textContent);
updateMainResultScreen(first);
}
else if(first != '' && second == '' && currentOperator != ''){
second = appendNumber(numberButton.textContent);
updateMainResultScreen(second);
}
else {
second = appendNumber(numberButton.textContent);
updateMainResultScreen(second);
}
})
})
operatorButtons.forEach(operatorButton => {
operatorButton.addEventListener("click", () => {
if(first != '' && second == ''){
currentOperator = operatorButton.getAttribute('id');
updateUpperResultScreen(first + ' ' + currentOperator);
updateMainResultScreen(0);
}
else if (second != '' && first != ''){
res = operate(first,currentOperator,second);
first = res;
currentOperator = operatorButton.getAttribute('id');
updateUpperResultScreen(first + ' ' + currentOperator);
updateMainResultScreen(0);
second = '';
}
else {
currentOperator = operatorButton.getAttribute('id');
updateUpperResultScreen(first + ' ' + currentOperator);
return;
}
})
})
clearButton.addEventListener("click", () => {
updateMainResultScreen(0);
updateUpperResultScreen('');
first = '';
second = '';
currentOperator = '';
})
deleteButton.addEventListener("click", () => {
if(first != ''){
if(first != '' && second == ''){
first = Math.floor(first/10);
mainResult.textContent = first;
}
else {
second = Math.floor(second/10);
mainResult.textContent = second;
}
} else {
return;
}
})
resultButton.addEventListener("click", () => {
if(first == '' && second == ''){
return;
}
if(first != '' && second == '') {
mainResult.textContent = first;
}
if(first != '' && second != ''){
res = operate(first,currentOperator,second);
upperResult.textContent = first + ' ' + currentOperator + ' ' + second + ' = '
mainResult.textContent = res;
first = res;
second = '';
}
})
commaButton.addEventListener("click", () => {
alert("Point currently out of Office :)");
});
window.addEventListener('keydown', handleKeyboardInput);
/******************************FUNCTIONS*******************************/
function operate(num1,operation,num2) {
switch (operation) {
case '+': return Number(num1)+Number(num2);
case '-': return Number(num1)-Number(num2);
case '/': return num2 == 0 ? ERROR_DIVIDE_BY_ZERO : num1/num2;
case '*': return num1 * num2;
default: return ERROR_NO_VALID_OPERATOR;
}
}
function appendNumber(input) {
// Cant input global variable, so same condition tree as eventlistener
//to decide if to var first oder var second should be appended/replaced
let num = 0;
if (first == '' && second == '') {num = first;}
else if (first != '' && second == '' && currentOperator == '') {num = first;}
else if (first != '' && second == '' && currentOperator != '') {num = second;}
else {num = second;}
if(input == 0 && num == ''){
alert('no leading zeros please.')
num = '';
}
num = mainResult.textContent === 0 ?
Number(input) : Number(String(num += input));
return num;
}
function updateMainResultScreen(num) {
mainResult.textContent = num;
}
function updateUpperResultScreen(str) {
upperResult.textContent = str;
}
function handleKeyboardInput(e) {
if (e.key >= 0 && e.key <= 9) document.getElementById(e.key).click();
if (e.key === '=' || e.key === 'Enter') document.getElementById('resultButton').click();
if (e.key === 'Backspace') document.getElementById('deleteButton').click();
if (e.key === 'Escape') document.getElementById('clearButton').click();
if (e.key === '+' || e.key === '-' || e.key === '*' || e.key === '/')
document.getElementById(e.key).click();
}
/****************************LAYOUT LISTENERS***********************************/
window.addEventListener("resize", () => {
infoBox.style.visibility = window.innerWidth < 1153 ? 'hidden':'visible';
})
// Display Number Overflow Handler with Mutationobserver
let observer = new MutationObserver(() => {
console.log(mainResult.clientWidth);
if(mainResult.clientWidth > document.getElementById('screen').clientWidth){
mainResult.textContent = '*OVERFLOW*';
upperResult.textContent = '';
}
})
let config = {characterData: false, subtree: true, childList: true};
observer.observe(mainResult,config);
/*
=== Leading Zero does not work with this concept ( You'll get an alert :) )===
BUG geteilt durch 0 macht keinen Fehler
BUG 4*0 + 3 (es geht nicht mit 0 sondern mit 4 weiter, sollte aber 0 + im upper sein)
================================================================================
TODO footer hinzufügen (schauen wie das so geht mit github logo etc, oder ist das bei GitHub Pages normal?)
*/