-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.js
192 lines (152 loc) · 4.9 KB
/
functions.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
// TODO: if sum-string-display is unpopulated, do not populate it (i.e. when entering first number)
// TODO: do not allow multiples of one real-world operation at a time (e.g. ++, --, /+, *+, etc.)
// TODO: incorporate BODMAS so the calculations are ACTUALLY CORRECT
// global variables
var numString = ""; // used to build string to be converted to float and stored in oldValue
var oldValue = 0.0;
var newValue = 0.0;
var result = 0.0;
var plusCheck, minusCheck, multiplyCheck, divideCheck = false;
var operator = "";
// updates variable and screen values to value of number button pressed
function returnNumber(num) {
if (document.getElementById('screen').value.length <= 9) {
if (numString == "") document.getElementById('screen').value = null;
document.getElementById('screen').value += num;
document.getElementById('screen').style.textAlign = "right";
document.getElementById('sum-string-display').innerHTML += num;
numString += num;
console.log("numString: " + numString);
console.log("oldValue: " + oldValue);
if (plusCheck) {
result = oldValue + Number(numString);
} else if (minusCheck) {
result = oldValue - Number(numString);
} else if (multiplyCheck) {
result = oldValue * Number(numString);
} else if (divideCheck) {
result = oldValue / Number(numString);
}
} else {
console.log("There is a number limit.");
}
};
// used by math operator buttons to set the operator variable
function setOperator (op) {
operatorCheck = true;
document.getElementById('screen').value = "";
document.getElementById('sum-string-display').innerHTML += op;
console.log(op);
switch (op) {
case "+":
newValue += Number(numString);
oldValue += newValue;
numString = "";
operator = '+';
plusCheck = true;
break;
case "-":
if (!oldValue > 0) {
newValue += Number(numString);
oldValue += newValue;
}
numString = "";
operator = '-';
minusCheck = true;
break;
case "*":
if (!oldValue > 0) oldValue += Number(numString);
numString = "";
operator = '*';
multiplyCheck = true;
break;
case "/":
if (!oldValue > 0) oldValue += Number(numString);
numString = "";
operator = '/';
multiplyCheck = true;
break;
}
}
// wipe set variable and screen values to 0
function clearAll() {
numString = "";
oldValue = 0.0;
result = 0.0;
operatorCheck = false;
document.getElementById('screen').style.textAlign = "right";
document.getElementById('screen').value = oldValue;
document.getElementById('sum-string-display').innerHTML = "";
console.log("Clear All");
}
// calculates final answer
function equals() {
plusCheck, minusCheck, multiplyCheck, divideCheck = false;
document.getElementById('screen').value = result;
document.getElementById('sum-string-display').innerHTML = "";
document.getElementById('screen').style.textAlign = "left";
oldValue = result;
numString = "";
console.log("---------------------------");
console.log("result: " + result);
console.log("numString: " + numString);
console.log("oldValue: " + oldValue);
console.log("---------------------------");
}
document.addEventListener("keydown", function (event) {
switch (event.key) {
case "1":
returnNumber(1);
// var numAudio = new Audio("assets/audio/number.wav");
// numAudio.play();
break;
case "2":
returnNumber(2);
break;
case "3":
returnNumber(3);
break;
case "4":
returnNumber(4);
break;
case "5":
returnNumber(5);
break;
case "6":
returnNumber(6);
break;
case "7":
returnNumber(7);
break;
case "8":
returnNumber(8);
break;
case "9":
returnNumber(9);
break;
case "0":
returnNumber(0);
break;
case "=": // using = for addition so user does not need a key combination
setOperator('+');
break;
case "-":
setOperator('-');
break;
case "x":
setOperator('*');
break;
case "/":
setOperator('/');
break;
case "Enter":
equals();
break;
case "c":
clearAll();
break;
default:
console.log("That key is not a number or operator!");
break;
}
});