-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
224 lines (184 loc) · 6.08 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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
TODO:
Limit number input
Clean up structure
*/
(function() {
"use strict";
// Shortcut to get elements
var el = function(element) {
if (element.charAt(0) === "#") { // If passed an ID...
return document.querySelector(element); // ... returns single element
}
return document.querySelectorAll(element); // Otherwise, returns a nodelist
};
// Variables
var viewer = el("#viewer"), // Calculator screen where result is displayed
equals = el("#equals"), // Equal button
nums = el(".num"), // List of numbers
ops = el(".ops"), // List of operators
theNum = "", // Current number
oldNum = "", // First number
resultNum, // Result
operator; // Batman
// When: Number is clicked. Get the current number selected
var setNum = function() {
//if . was pressed, then only do this if theNum doesn't already include a .
if ((this.getAttribute("data-num")==="." && theNum.indexOf(".") === -1) || this.getAttribute("data-num")!=="."){
if (resultNum) { // If a result was displayed, reset number
theNum = this.getAttribute("data-num");
resultNum = "";
} else { // Otherwise, add digit to previous number (this is a string!)
theNum += this.getAttribute("data-num");
}
viewer.innerHTML = theNum; // Display current number
}
};
// When: Number key is pressed. Get the current number selected
var setNumKey = function(keyPressed) {
//if . was pressed, then only do this if theNum doesn't already include a .
if ((keyPressed ==="." && theNum.indexOf(".") === -1) || keyPressed !=="."){
if (resultNum) { // If a result was displayed, reset number
theNum = keyPressed;
resultNum = "";
} else { // Otherwise, add digit to previous number (this is a string!)
theNum += keyPressed;
}
viewer.innerHTML = theNum; // Display current number
}
}
// When: Operator is clicked. Pass number to oldNum and save operator
var moveNum = function() {
oldNum = theNum;
theNum = "";
operator = this.getAttribute("data-ops");
equals.setAttribute("data-result", ""); // Reset result in attr
};
// When: Operator is clicked. Pass number to oldNum and save operator
var moveNumKey = function(wordOperator) {
oldNum = theNum;
theNum = "";
operator = wordOperator;
equals.setAttribute("data-result", ""); // Reset result in attr
};
// When: Equals is clicked. Calculate result
var displayNum = function() {
// Convert string input to numbers
oldNum = parseFloat(oldNum);
theNum = parseFloat(theNum);
// Perform operation
switch (operator) {
case "plus":
resultNum = oldNum + theNum;
break;
case "minus":
resultNum = oldNum - theNum;
break;
case "times":
resultNum = oldNum * theNum;
break;
case "divided by":
resultNum = oldNum / theNum;
break;
case "square":
resultNum = oldNum ** 2;
break;
case "cube":
resultNum = oldNum ** 3;
break;
case "square root":
resultNum = Math.sqrt(oldNum);
break;
case "power":
resultNum = oldNum ** theNum;
break;
case "sin":
resultNum = Math.sin(oldNum);
break;
case "cos":
resultNum = Math.cos(oldNum);
break;
case "tan":
resultNum = Math.tan(oldNum);
break;
// If equal is pressed without an operator, keep number and continue
default:
resultNum = theNum;
}
// If NaN or Infinity returned
if (!isFinite(resultNum)) {
if (!isInt(resultNum) || !isFloat(resultNum)) { // If result is not a number; set off by, eg, double-clicking operators
resultNum = "You broke it!";
} else { // If result is infinity, set off by dividing by zero
resultNum = "Look at what you've done";
el('#calculator').classList.add("broken"); // Break calculator
el('#reset').classList.add("show"); // And show reset button
}
}
// Display result, finally!
viewer.innerHTML = resultNum;
equals.setAttribute("data-result", resultNum);
// Now reset oldNum & keep result
oldNum = 0;
theNum = resultNum;
};
// When: Clear button is pressed. Clear everything
var clearAll = function() {
oldNum = "";
theNum = "";
viewer.innerHTML = "0";
equals.setAttribute("data-result", resultNum);
};
// When: Key is pressed, find out if it's a valid one and send to relevant function if so
var findKey = function(e){
if((e.key >= 0 && e.key <= 9) || e.key == "."){
setNumKey(e.key);
}
// Operator keys
switch (e.key) {
case "+":
moveNumKey("plus");
break;
case "-":
moveNumKey("minus");
break;
case "*":
moveNumKey("times");
break;
case "/":
moveNumKey("divided by");
break;
case "=":
case "Enter":
displayNum();
break;
case "Escape":
case "c":
case "C":
clearAll();
break;
// if another key is pressed then do nothing
default:
break;
}
}
/* The click events */
// Add click event to numbers
for (var i = 0, l = nums.length; i < l; i++) {
nums[i].onclick = setNum;
}
// Add click event to operators
for (var i = 0, l = ops.length; i < l; i++) {
ops[i].onclick = moveNum;
}
//Add keyboard event to use numbers and basic operators
document.onkeyup = findKey;
// Add click event to equal sign
equals.onclick = displayNum;
// Add click event to clear button
el("#clear").onclick = clearAll;
// Add click event to reset button
el("#reset").onclick = function() {
window.location = window.location;
};
}());