-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
229 lines (215 loc) · 7.82 KB
/
Copy pathscript.js
File metadata and controls
229 lines (215 loc) · 7.82 KB
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
const DISPLAY_MAX_LENGTH = 11;
function inputSwitch(input){
//input switch - performs actions corresponding to the keypad input
switch(input){
case '+':
isOperandChosen = true;
chosenOperand = '+';
calcString();
return false;
break;
case '-':
isOperandChosen = true;
chosenOperand = '-';
calcString();
return false;
break;
case 'x':
isOperandChosen = true;
chosenOperand = 'x';
calcString();
return false;
break;
case '/':
isOperandChosen = true;
chosenOperand = '/';
calcString();
return false;
break;
case '=':
operationArray.push(inputArray.join(''));
//goes through each entry in operationArray and evaluates the two parameters with the operator
performOps(operationArray);
operationArray = []; //empty operations array so continuous evaluation doesn't just compound the current integers
return false;
break;
case '.':
//pushes a decimal dot to both input and display arrays if there isn't one already
if(!hasDecimalDot(input)){
pushToInputDisplay('.');
}else{
//this is so the character display count doesn't falsely increase
return false;
}
break;
default:
if(inputArray[0]==0){
inputArray.splice(0,1);
}
//if an operand has been chosen, empty the display when entering the next "step" of the calculation
//when you choose an operand, it pushes the current input data to the operation array. this also makes it so you can apply additional calculations to the result of the original evaluation
if(isOperandChosen){
operationArray.push(inputArray.join(''), chosenOperand);
isOperandChosen = false;
clearDisplay();
}
if(isEvalDone && !isOperandChosen){
isEvalDone = false;
clearDisplay();
}
//
// else if(isEvalDone&&isOperandChosen){
// operationArray.push(inputArray.join(''), chosenOperand);
// clearDisplay();
// }
//removes leading zero from cleared display
if(displayArray[0] == 0 && displayArray.length === 1){
displayArray.splice(0,1);
}
//pushes integers to both input and display arrays
pushToInputDisplay(input);
return true;
break;
}
}
//short function to flush the display
function emptyDisplay(){
$('.calc-display').empty()
}
//short function for appending to display
function appendToDisplay(input){
$('.calc-display').append(input)
}
//short function for pushing to input array
function pushToInput(input){
inputArray.push(input);
}
//short function for pushing to display array
function pushToDisplay(input){
if(displayArray[0]==0){displayArray.splice(0,1);}
displayArray.push(input)
}
//calls both array push functions
function pushToInputDisplay(input){
pushToInput(input);
pushToDisplay(input);
}
//checks whether or not the decimal dot is included in the current inputarray
function hasDecimalDot(input){
if(!inputArray.includes(input)){
return false;
}
return true;
}
//checks the current number of display characters - deletes the first index of display numbers if numbers exceed display character limit
function checkDisplayCharacterNum(){
if(inputArray[0] != 0){
currentNumbersOnDisplay++;
}
if(currentNumbersOnDisplay > DISPLAY_MAX_LENGTH){
displayArray.splice(0,1);
currentNumbersOnDisplay=DISPLAY_MAX_LENGTH;
}
}
//clears the display and all associated arrays, as well as resetting current amount of numbers displayed
function clearDisplay(){
currentNumbersOnDisplay = 0;
inputArray = [];
displayArray = [0];
emptyDisplay();
}
function calcString(){
if(operationArray.length >= 2 && isOperandChosen){
operationArray.push(inputArray.join(''));
performOps(operationArray);
operationArray = [];
// isOperandChosen = false;
isEvalDone = false;
}
}
//performs the operations on the input numbers
function performOps(inArray){
let a = 0;
let b = 0;
let op = '';
inArray.forEach(function(x,i){
//even index are nums, odd index are operators
if(i%2!==0){ //even index, op
op = x;
}else if(i%2===0 && !a && !b){ //uneven index & unset a, num
a = x;
}else if(i%2===0 && !b){
b = x;
}
if(a && b){
let result = doMath[op](a,b);
//if result is longer than 11 characters, chop off anything after 11
if(result.toString().length > DISPLAY_MAX_LENGTH){
//breaks the result into a string and removes all integers after display limit
result = result.toString().split('').slice(0,DISPLAY_MAX_LENGTH);//.join('');
//increments the last number in result if there's a decimal somewhere
if(result.includes('.')){
//gets the last integer of the result and increments it
let lastInt = result[DISPLAY_MAX_LENGTH-1];
lastInt++;
//removes the last integer of the result and pushes in the newly rounded integer
result.splice(DISPLAY_MAX_LENGTH-1,1);
result.push(lastInt);
}
//turns the result back into a float
result = parseFloat(result.join(''));
}
clearDisplay();
pushToInputDisplay(result);
isEvalDone = true;
}
});
}
//math function - object containing multiple arithmetic functions
//doMath[operand](x,y);
let doMath = {
'+':function(x,y){return parseFloat(x)+parseFloat(y);},
'-':function(x,y){return parseFloat(x)-parseFloat(y);},
'/':function(x,y){
if(x==0||y==0){
return "DIVISION BY 0";
}
return parseFloat(x)/parseFloat(y);},
'x':function(x,y){return parseFloat(x)*parseFloat(y);},
}
//array setup
let inputArray = []; //stores keypad input data
let displayArray = [0]; //stores display data
let operationArray = []; //stores data for arithmetic operations
//checks whether or not evaluation is complete
let isEvalDone = false;
//checks whether or not an operand has been chosen
let isOperandChosen = false;
//sets the chosen operand
let chosenOperand = 0;
//keeps track of the current numbers on display
let currentNumbersOnDisplay = 0
$('.button').on('mousedown',function(){
//grabs the value (0-9, arithmetic operands) from the button being clicked
let input = $(this).attr('data-set');
//stores true or false from the input switch - REQUIRED for only one decimal dot
let switchResult = inputSwitch(input);
//only checks and deletes display characters if switch returns true
//any decimals after 1, and all operators returns false
if(switchResult){checkDisplayCharacterNum();}
//calculates the current inputs in operationArray if an operand has been pressed AND if there's already 2 inputs
// calcString();
//flushes the display before rendering new data
emptyDisplay();
appendToDisplay(displayArray);
});
$('.clear').on('mousedown',function(){
//clears both input and display array as well as flushes display
operationArray = [];
clearDisplay();
appendToDisplay(displayArray);
});
$('.junq').on('click',function(){
window.open('https://github.com/junkdeck/','_blank');
});
appendToDisplay(0);