forked from thinkswell/javascript-mini-projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
61 lines (52 loc) · 1.58 KB
/
script.js
File metadata and controls
61 lines (52 loc) · 1.58 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
const addItem = (item) => {
const inputEl = document.getElementById("input-el")
const realInput = document.getElementById("real-input-el")
const specials = ["*", "/", "-", "+"]
const currentValue = inputEl.value
const replacements = {
"*": "×",
"-": "−",
"/": "÷",
"+": "+",
"+": "+",
}
if(specials.includes(item)) {
inputEl.value += replacements[item]
realInput.value += item
return
}
if (item === '.') {
const numberSegments = currentValue.split(/[\*\+\-\/]/)
const currentSegment = numberSegments.at(-1)
if (currentSegment.includes('.')) {
return;
}
}
const lastChar = currentValue.slice(-1)
if (specials.includes(item) && specials.includes(lastChar)) {
return
}
realInput.value += item
inputEl.value += item
}
const clearScreen = () => {
document.getElementById("input-el").value = ""
document.getElementById("real-input-el").value = ""
}
const deleteEl = () => {
const inputEl = document.getElementById("input-el")
const realInput = document.getElementById("real-input-el")
inputEl.value = inputEl.value.slice(0, -1)
realInput.value = realInput.value.slice(0, -1)
}
const equal = () => {
const realInput = document.getElementById("real-input-el")
const inputEl = document.getElementById("input-el")
try {
realInput.value = eval(realInput.value)
inputEl.value = realInput.value
} catch (err) {
realInput.value = "Error"
inputEl.value = "Error"
}
}