-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcal.js
270 lines (223 loc) · 11.7 KB
/
cal.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
Author: github.com/iamjam2
Description: JavaScript file for GitHub Project - Basic calculator made with web technologies
Licence: None... knock yourself out if you wanna use this for anything.
*/
$(document).ready(function(){
var screenText = "";
var castedValue = 0;
const calExpr = [];
const operators = [];
function updateScreen(){$("#cal-screen").val(screenText);} //Updates screen from screenText variable...
function isNumber(testChar){
var numberRegEx = /[0-9]/g; //Regular expression for digits 0 - 9 with global search modifier...
return numberRegEx.test(testChar);
}
function screenHasAnswer(){
var equalToRegEx = /=/m; //Regular expression to find the equal character with multi-line search modifier...
return equalToRegEx.test(screenText);
}
function lastIsNumber(){ //Checks if the last character on screen is a number...
var lastChar = screenText.slice(screenText.length - 1);
return isNumber(lastChar); //True or False return from function with RegEx
}
function lastIsPower(){ //Checks if the last character on the screen is a power...
var lastChar = screenText.slice(screenText.length - 1);
return lastChar == "²";
}
function lastIsPeriod(){ //Checks if the last character on the screen is a period...
var lastChar = screenText.slice(screenText.length - 1);
return lastChar == ".";
}
function addToExpr(inputValue){ //Adds values and characters to an array holding the full expression/equation to be calculated...
calExpr.push(inputValue);
castedValue = 0;
}
function popFromExpr(){ //removes items from the arrays correctly as per the displayed expression/equation, when backspace is used..
var exprLength = screenText.length;
if(exprLength < operators[operators.length - 1]){
calExpr.pop(); calExpr.pop();
operators.pop();
}
}
function doCal(operator, position){ //Peforms the calculations from what's stored on the array...
var calDone = false; //Confirms that the calculation has been done and saved to the array correctly (Error handling to be added here)...
switch(operator){ //Calculations are done and the answer is saved to the array block on the left side, ex: [1][+][2] becomes [3][+][2]
case "/":
calExpr[position - 1] = calExpr[position - 1] / calExpr[position + 1];
calDone = true;
break;
case "%":
calExpr[position - 1] = calExpr[position - 1] % calExpr[position + 1];
calDone = true;
break;
case "x":
calExpr[position - 1] = calExpr[position - 1] * calExpr[position + 1];
calDone = true;
break;
case "+":
calExpr[position - 1] = calExpr[position - 1] + calExpr[position + 1];
calDone = true;
break;
case "-":
calExpr[position - 1] = calExpr[position - 1] - calExpr[position + 1];
calDone = true;
break;
default:
//No default case, input is limited by the buttons on the calculator...
}
calExpr.splice(position + 1, 1); //Removes the array value on the right side block after the calculation is performed, ex: the above [3][+][2] becomes [3][+]
calExpr.splice(position, 1); //Removes the operator from the array after the calculation is performed, ex: the above [3][+] becomes [3] the answer from the calculation.
return calDone;
}
function equalTo(){ //Runs when the equal button is pressed...
castedValue = 0;
if(!(screenHasAnswer())){
var solution = ""
var storedExprLength = calExpr.length;
if((lastIsNumber() || lastIsPower()) && !(screenText == "")){ //Last character on screen must be either a number or a power or the screen and the screen must not be blank...
if(storedExprLength == 0){ //If nothing is stored on the array yet then no operator has been added to the calculation.
if(lastIsPower()){ //Only the square of a number is being calculated...
castedValue = Number(screenText.slice(0, screenText.length - 1));
castedValue = Math.pow(castedValue,2);
addToExpr(castedValue);
solution = "= " + String(calExpr[0]);
calExpr.pop();
}
else{ //Only a plain number is on the screen...
addToExpr(Number(screenText));
solution = "= " + String(calExpr[0]);
calExpr.pop();
}
}
else{ //Else there is an operator in the calculation...
if(lastIsPower()){
castedValue = Number(screenText.slice(operators[operators.length -1] + 1, screenText.length - 1));
castedValue = Math.pow(castedValue,2);
addToExpr(castedValue);
}
else{
castedValue = Number(screenText.slice(operators[operators.length -1] + 1, screenText.length));
addToExpr(castedValue);
}
storedExprLength = calExpr.length; //The last value is added to the array, update the saved length of the array..
//Loop through the array and perform calculations according to the correct mathematical precidence...
for(let count = 1; count < storedExprLength -1 ; count += 2){
if(calExpr[count] == "/"){ if(doCal("/", count)){ count -= 2; storedExprLength = calExpr.length;}}
}
for(let count = 1; count < storedExprLength -1 ; count += 2){
if(calExpr[count] == "%"){ if(doCal("%", count)){ count -= 2; storedExprLength = calExpr.length;}}
}
for(let count = 1; count < storedExprLength -1 ; count += 2){
if(calExpr[count] == "x"){ if(doCal("x", count)){ count -= 2; storedExprLength = calExpr.length;}}
}
for(let count = 1; count < storedExprLength -1 ; count += 2){
if(calExpr[count] == "+"){ if(doCal("+", count)){ count -= 2; storedExprLength = calExpr.length;}}
}
for(let count = 1; count < storedExprLength -1 ; count += 2){
if(calExpr[count] == "-"){ if(doCal("-", count)){ count -= 2; storedExprLength = calExpr.length;}}
}
//After these loops, only 1 array block remains with the final answer...
solution = "= " + String(calExpr[0]);
calExpr.pop();
}
}
else solution = "Error, invalid input!"; //The entered expression/equation cannot be calculated...
screenText += "\n\n" + solution; //Display the solution calculated or feedback...
updateScreen();
}
}
function clearAll(){ //Clears the screen and all arrays...
screenText = "";
while(operators.length > 0){operators.pop();}
while(calExpr.length > 0){calExpr.pop();}
updateScreen();
}
function backspace(){ //Clears the last character on screen or the entire screen and arrays if there was a solution displayed...
if(screenHasAnswer()){
clearAll();
}
else{
var tempText = "";
if(!(screenText == "")) tempText = screenText.slice(0, screenText.length - 1);
tempText = tempText.trim();
screenText = tempText;
popFromExpr()
updateScreen();
}
}
function btnPress(pressedChar){ //Computes what to do with each button pressed...
if(screenHasAnswer()){ //If an answer is displayed, the screen and arrays should first be cleared...
clearAll();
}
if(isNumber(pressedChar)){ //If the button pressed is a number...
if(!(lastIsPower())){
if(!(screenText == "" || lastIsNumber() || lastIsPeriod())){//Screen is not blank or the last char on screen is not a number
pressedChar = " " + pressedChar;
}
screenText += pressedChar;
}
}
else{ //If the button pressed is not a number...
if(!(screenText == "") && (lastIsNumber() || lastIsPower())){//Non-numeric chars can only go after numbers and powers
if(pressedChar == "^"){//If non-numeric character is a power
if(!(lastIsPower())) screenText += "²";
}
else if(pressedChar == "."){//If non-numeric character is a period
if(!(lastIsPower())) screenText += ".";
}
else{//If non-numeric character pressed is not either a power or a period
if(operators.length === 0){//if there's no operator(s) in the expression already
if(lastIsPower()){
castedValue = Number(screenText.slice(0, screenText.length - 1));
castedValue = Math.pow(castedValue,2);
addToExpr(castedValue);
}
else{
castedValue = Number(screenText.slice(0, screenText.length));
addToExpr(castedValue);
}
}
else{//If there already is an operator in the expression
if(lastIsPower()){
castedValue = Number(screenText.slice(operators[operators.length -1] + 1, screenText.length - 1));
castedValue = Math.pow(castedValue,2);
addToExpr(castedValue);
}
else{
castedValue = Number(screenText.slice(operators[operators.length -1] + 1, screenText.length));
addToExpr(castedValue);
}
}
screenText += " " + pressedChar;
operators.push(screenText.length);
addToExpr(pressedChar);
}
}
}
updateScreen();
}
//********************************Button click functions********************************
$("#clear-all").click(function(){ clearAll();})
$("#delete").click(function(){ backspace();})
$("#equal-to").click(function(){ equalTo();})
//Digit buttons
$("#one").click(function(){ btnPress("1");})
$("#two").click(function(){ btnPress("2");})
$("#three").click(function(){ btnPress("3");})
$("#four").click(function(){ btnPress("4");})
$("#five").click(function(){ btnPress("5");})
$("#six").click(function(){ btnPress("6");})
$("#seven").click(function(){ btnPress("7");})
$("#eight").click(function(){ btnPress("8");})
$("#nine").click(function(){ btnPress("9");})
$("#zero").click(function(){ btnPress("0");})
$("#period").click(function(){ btnPress(".")})
//Operand buttons
$("#plus").click(function(){ btnPress("+");})
$("#minus").click(function(){ btnPress("-");})
$("#multiply").click(function(){ btnPress("x");})
$("#divide").click(function(){ btnPress("/");})
$("#modulus").click(function(){ btnPress("%");})
$("#power-two").click(function(){btnPress("^");})
})