-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
139 lines (138 loc) · 4.22 KB
/
app.js
File metadata and controls
139 lines (138 loc) · 4.22 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
//main display
var display = document.getElementsByClassName('display')[0];
//sub display
var displaySub = document.getElementsByClassName('display-sub')[0];
//chain of operations, default is zero
var opChain = [0];
//keeps track of whether solution is returned
var solutionReturned = false;
//all buttons
var btns = document.getElementsByTagName('button');
//add click events to each button
for (var i = 0; i < btns.length; i++) {
btns[i].addEventListener('click', getBtnValue);
}
//get button value and send it to be processed
function getBtnValue(e) {
var entry = e.currentTarget.value;
processInput(entry);
}
//process input based on button pressed, perform next appropriate operation
function processInput(input) {
switch (input) {
case "=":
calculate();
break;
case "ce":
clearEntry();
break;
case "ac":
clearAll();
break;
case "/":
case "*":
case "+":
case "-":
case ".":
case "0":
validateInput(input);
break;
default:
updateDisplay(input);
}
}
//function determines how display will be updated
function updateDisplay(input) {
//if display is blank and input isn't zero or solution has been returned
if ((display.innerHTML === "0" && input !== "0") || opChain === 0 || solutionReturned === true) {
//if solution is displayed and input is an operator or display is blank and input is an operator
if ((solutionReturned === true && !isNumeric(input)) || (display.innerHTML === "0" && !isNumeric(input))) {
//set solution to false because operation chain is increasing
solutionReturned = false;
//add input to the chain
chainOperation(input);
//add input to the display
display.innerHTML += input;
} else {
//if above conditions not met, clear the chain, input replaces blank display state of 0 and replaces default 0 in operation chain
solutionReturned = false;
clearOpChain();
display.innerHTML = input;
opChain.push(input);
}
} else {
//if not starting from blank state, add input to display and operation chain
display.innerHTML += input;
chainOperation(input);
}
displaySub.innerHTML = opChain.join("");
}
//function validates 0, operators, and decimals
function validateInput(input) {
var currentDisplay = display.innerHTML;
//if input is zero and display isn't, or input is the first decimal in display and solution is not displayed
if ((input === "0" && currentDisplay !== "0") || (input === "." && currentDisplay.indexOf(".") < 0 && solutionReturned === false)) {
//update display
updateDisplay(input);
}
//if input is an operator
if (input === "/" || input === "*" || input === "-" || input === "+") {
var lastChar = opChain[opChain.length - 1];
//if last character was a number
if (isNumeric(lastChar)) {
//update display
updateDisplay(input);
}
}
}
//checks whether given character is a number
function isNumeric(value) {
return (value == Number(value));
}
//clears last entry
function clearEntry() {
//if chain of operations is not empty
if (opChain.length >= 2) {
//remove last character
display.innerHTML = display.innerHTML.slice(0, -1);
} else {
//or set display to default 0
display.innerHTML = "0";
}
//remove last character from operation chain
opChain.pop();
//update sub display
displaySub.innerHTML = opChain.join("");
}
//clears chain of operations
function clearOpChain() {
opChain.length = 0;
}
//clears display, sub display, and sets chain of operations to default 0
function clearAll() {
opChain = [0];
display.innerHTML = "0";
displaySub.innerHTML = "";
}
//adds to chain of operations
function chainOperation(input) {
opChain.push(input);
}
//returns solution
function calculate() {
//if operation chain isn't empty
if (opChain !== 0) {
//evaluate operation chain
var solution = eval(opChain.join(""));
//update display with solution
display.innerHTML = solution;
//update display to reflect previous operations
displaySub.innerHTML += "=";
//reset operation chain
opChain.length = 0;
//operation chain now contains solution
opChain.push(solution);
//set global variable to indicate solution was reached
solutionReturned = true;
}
}