-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
173 lines (109 loc) · 4.22 KB
/
main.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
var sum = 0;
//what's being displayed
var display = 0;
var numberInput = 0;
//display integer being transferred into a variable
var numberStore1 = 0;
var numberStore2 = 0;
//variable to store operator
var operatorStore = " ";
var btnTextArray = ['C', ' ', ' ', '/', '7', '8', '9', 'x', '4', '5', '6', '-', '1', '2', '3', '+', '0', '.', ' ', '='];
//UI
function calculator() {
var displayRow = document.createElement("div");
displayRow.id = 'drowid';
displayRow.className = 'row justify-content-center';
var displayCol = document.createElement("div");
displayCol.id = "dcolid";
displayRow.className = 'col-9 ml-5 mt-5';
displayCol.innerHTML = display;
var calcContainer = document.createElement("div")
calcContainer.id = 'calcContainerId';
calcContainer.className = "container mt-5";
var btnRow = document.createElement("div");
btnRow.id = 'btnRowid';
btnRow.className = 'row justify-content-center d-flex flex-grow-1 mx-3 my-3';
//setting up buttons and click function to control what happens on clicks
var i = 0
while (i < 20) {
var btnText = document.createElement("div");
btnText.addEventListener('click', clickEvent);
//creatina a button text array globally, and then setting the btn text to each index which runs through the loop.
btnText.id = btnTextArray[i];
btnText.setAttribute('class', 'col-3 border rounded-circle circle display-4');
btnText.innerHTML = btnTextArray[i];
// btnTextArray.push(0);
btnRow.appendChild(btnText);
i++;
}
displayRow.appendChild(displayCol);
calcContainer.appendChild(displayRow);
calcContainer.appendChild(btnRow);
document.getElementById('calculatorhtml').appendChild(calcContainer);
}
//
function clickEvent() {
//index of array set to a variable called keypressed
keypressed = (this.id);
//cases for each type
switch (keypressed) {
//clears the display
case 'C':
operatorStore = " ";
numberStore1 = 0;
numberStore2 = 0;
display = 0;
sum = 0;
numberInput = 0;
break;
case '+': case '-': case 'x': case '/':
// runs second. first time through operator is open. second time it's not.if a operator is hit and it isn't a emtpy string, which happens after first click, next click evaluates
if (operatorStore != " ") {
evaluate()
}
//storing first integer into a new variable, and resetting display to 0.
numberStore1 = numberInput;
numberInput = 0;
operatorStore = keypressed;
break;
case '=':
evaluate();
break;
// 1st. MY NUMBERS CASE and decimal CASE. set numbers to default. Default says other keyspressed=this.id not defined in cases, do this.
default:
//if display shows 0 it gets rid of it when you press a number.
if (numberInput == 0) {
numberInput = " "
}
//then display is nothing plus the integer you select, and if not 0 keeps adding to display..999999. 2nd number to equate is reloaded into numberInput for numberStore2
numberInput += keypressed;
//numberInput is display
display = numberInput;
break;
}
//set innerhtml to the display variable
dcolid.innerHTML = display;
}
function evaluate() {
numberStore2 = numberInput.trim();
if (numberStore2 == '.') {
display = "Josh made error"
}
else if (operatorStore == "+") {
sum = Number(numberStore1) + Number(numberStore2);
display = Number(sum.toPrecision(12));
}
else if (operatorStore == "-") {
sum = Number(numberStore1) - Number(numberStore2);
display = Number(sum.toPrecision(12));
}
else if (operatorStore == "x") {
sum = Number(numberStore1) * Number(numberStore2);
display = Number(sum.toPrecision(12));
}
else if (operatorStore == "/") {
sum = Number(numberStore1) / Number(numberStore2);
display = Number(sum.toPrecision(12));
}
numberInput = display;
}