-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
143 lines (118 loc) · 3.92 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
const operatorBtns = document.querySelectorAll('.operators');
const numberBtns = document.querySelectorAll('.number');
const decimalBtn = document.querySelector('.decimal');
const resultBtn = document.querySelector('.result');
const allClearBtn = document.getElementById('all-clear');
const clearBtn = document.getElementById('only-clear');
let inputField;
document.addEventListener('DOMContentLoaded', () => {
inputField = document.getElementById('input-value');
handleKeyboardInput();
inputField.focus();
});
window.addEventListener('resize', handleKeyboardInput);
function handleKeyboardInput() {
if (window.innerWidth <= 500) {
inputField.setAttribute('readonly', true);
// inputField.addEventListener('keydown', preventKeyboardInput);
} else {
inputField.removeAttribute('readonly');
// inputField.removeEventListener('keydown', preventKeyboardInput);
}
inputField.focus();
}
// function preventKeyboardInput(event) {
// event.preventDefault();
// }
function handleInput(event) {
const validKeys = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'Backspace', 'ArrowLeft', 'ArrowRight', '+', '-', '*', '/', '.', '='];
const value = inputField.value;
if (!validKeys.includes(event.key)) {
event.preventDefault();
}
if (event.key === '.') {
const lastPart = value.split(/[\+\-\*\/]/).pop();
if (lastPart && !lastPart.includes('.')) {
return;
}
else if (lastPart === "") {
inputField.value += "0.";
event.preventDefault();
}
else {
event.preventDefault();
}
}
if (event.key === 'Enter' || event.key === '=') {
event.preventDefault(); // to prevent = appending after the result is displayed
inputField.value = evaluate(inputField.value);
}
if (isOperator(event.key)) {
console.log(value.length);
if (value.length == 0) {
inputField.value = "0";
}
else if (value.length > 0 && isOperator(value.charAt(value.length - 1))) {
event.preventDefault();
}
}
}
function isOperator(key) {
const operators = ['+', '-', '*', '/'];
if (operators.includes(key))
return true;
return false;
}
function evaluate(expression) {
let result = eval(expression);
if (result === Infinity || result === -Infinity) {
window.alert("Error: Cannot divide by zero");
return "";
}
result = parseFloat(result.toFixed(2));
return result;
}
numberBtns.forEach((numberBtn) => {
numberBtn.addEventListener("click", () => {
inputField.value += numberBtn.textContent;
inputField.focus();
});
});
operatorBtns.forEach((operatorBtn) => {
operatorBtn.addEventListener("click", () => {
const value = inputField.value;
if (value.length == 0) {
inputField.value = "0" + operatorBtn.textContent;
}
else if (value.length > 0 && !isOperator(value.charAt(value.length - 1))) {
inputField.value += operatorBtn.textContent;
}
inputField.focus();
});
});
decimalBtn.addEventListener("click", () => {
const value = inputField.value;
const lastPart = value.split(/[\+\-\*\/]/).pop();
if (lastPart && !lastPart.includes('.')) {
inputField.value += ".";
}
else if (lastPart === "") {
inputField.value += "0.";
}
inputField.focus();
});
allClearBtn.addEventListener("click", () => {
inputField.value = "";
inputField.focus();
});
clearBtn.addEventListener("click", () => {
var currInputValue = document.getElementById('input-value').value;
console.log(currInputValue);
if (currInputValue.length > 0) {
inputField.value = currInputValue.substring(0, currInputValue.length - 1);
}
inputField.focus();
});
resultBtn.addEventListener('click', () => {
inputField.value = evaluate(inputField.value);
});