-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculator.js
61 lines (49 loc) · 1.06 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
let checkBox = document.getElementById("checkbox");
function change() {
if (checkBox.checked == true) {
color = "green";
} else {
color = "white";
}
document.body.style.backgroundColor = color;
}
//the string that appears
const firstHalf = [];
let text = "";
function numbers(value) {
text += value;
document.getElementById("solution").innerHTML = text;
firstHalf.push(text);
}
function answer() {
let lastItem = firstHalf.findLast((x) => (x = firstHalf.length));
document.getElementById("solution").innerHTML = eval(lastItem);
text = eval(lastItem);
}
function clearField() {
document.getElementById("solution").innerHTML = 0;
text = "";
}
document.onkeydown = function (e) {
if (e.key in ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]) {
numbers(e.key);
}
if (e.key == "+") {
numbers("+");
}
if (e.key == "-") {
numbers("-");
}
if (e.key == "*") {
numbers("*");
}
if (e.key == "/") {
numbers("/");
}
if (e.key == ".") {
numbers(".");
}
if (e.key == "Enter") {
answer();
}
};