-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
139 lines (114 loc) · 3.31 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
var buttonCode = {
"ac": "ac",
"ce": "ce",
"divide": "/",
"multiply": "*",
"seven": "7",
"eight": "8",
"nine": "9",
"subtract": "-",
"four": "4",
"five": "5",
"six": "6",
"add": "+",
"one": "1",
"two": "2",
"three": "3",
"equals": "equals",
"zero": "0",
"dot": "."
}
var buttonRevCode = {
"ac": "ac",
"ce": "ce",
"/": "divide",
"*": "multiply",
"7": "seven",
"8": "eight",
"9": "nine",
"-": "subtract",
"4": "four",
"5": "five",
"6": "six",
"+": "add",
"1": "one",
"2": "two",
"3": "three",
"equals": "equals",
"0": "zero",
".": "dot"
}
var equation = "";
var lastInput = "";
var dotPressed = false;
var buttons = document.querySelector("#buttons");
var screen = document.querySelector("input[type=text]");
console.log(screen);
var operatorBtns = document.querySelectorAll(".operator");
var numberBtns = document.querySelectorAll(".number");
var dotBtn = document.getElementById("dot");
function animateBtn(btn) {
var button = document.getElementById(buttonRevCode[btn]);
if(button != null) {
console.log("Pushed: ", button);
button.classList.add("pushed");
window.setTimeout(function() {
button.classList.remove("pushed");
}, 100);
}
}
var noDot = false;
var noOp = true;
function keyPressed(buttonPressed) {
console.log(buttonPressed);
animateBtn(buttonPressed);
var opSyms = ["/", "-", "+", "*"];
var numKeys = ['0','1','2','3','4','5','6','7','8','9'];
var isDot = buttonPressed === ".";
var isOp = opSyms.indexOf(buttonPressed) > -1;
// if(!(noOp && isOp) && !(noDot && isDot)) {
if(buttonPressed == "ac") {
screen.value = "";
equation = "";
noDot = false;
noOp = true;
} else if(buttonPressed == "ce") {
var deleted = equation[equation.length-1];
equation = equation.slice(0, equation.length-1);
screen.value = equation;
if(deleted === ".") noDot = false;
if(opSyms.indexOf(deleted) > -1) noOp = false;
if(equation === "") noOp = true;
} else if(buttonPressed == "equals" && !noOp) {
var answer = eval(equation);
answer = +answer.toFixed(8);
equation = answer.toString();
screen.value = equation;
noOp = false;
noDot = true;
} else if (buttonPressed === "." && !noDot) {
equation += buttonPressed;
screen.value = equation;
noDot = true;
} else if (opSyms.indexOf(buttonPressed) > -1 && !noOp) {
equation += buttonPressed;
screen.value = equation;
noOp = true;
noDot = false;
} else if(numKeys.indexOf(buttonPressed) > -1) {
equation += buttonPressed;
screen.value = equation;
noOp = false;
}
// }
}
buttons.addEventListener("click", function(e) {
keyPressed(buttonCode[e.target.id]);
});
document.addEventListener("keydown", function(e) {
var key = e.key;
if(key == "Enter" || key == "=") key = "equals";
else if (key == "Backspace") key = "ce";
else if (key == "Escape") key = "ac";
keyPressed(key);
});