-
Notifications
You must be signed in to change notification settings - Fork 0
/
Super-Calculator.html
210 lines (187 loc) · 6.26 KB
/
Super-Calculator.html
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
<!DOCTYPE html>
<html>
<head>
<title>Super Calculator</title>
<link href='https://fonts.googleapis.com/css?family=Quantico' rel='stylesheet'>
<link href="navstyling.css" rel="stylesheet" type="text/css">
</head>
<body>
<ul>
<li id="main"><a href="Calculators Main Site.html">Calculators</a></li>
<li id="super"><a href="Super-Calculator.html">Super Calculator</a></li>
<li class="dropdown">
<a href="" class="dropbtn">Math</a>
<div class="dropdown-content">
<a href="./Math/Pythagorean.html">Pythagorean Theorem</a>
<a href="./Math/Quadratic.html">Quadratic Equation</a>
<a href="./Math/Midpoint.html">Midpoint Formula</a>
<a href="./Math/Distance.html">Distance Formula</a>
<a href="./Math/Slope.html">Slope Formula</a>
<a href="./Math/RadianDegree.html">Degree-Radian</a>
<a href="./Math/PercentChange.html">Percent Change</a>
</div>
</li>
<li class="dropdown">
<a href="" class="dropbtn">Science</a>
<div class="dropdown-content">
<a href="./Science/SolutePot.html">Solute Potential</a>
</div>
</li>
</ul>
<div id="calculator">
<header>
<h1>Super-Calculator</h1>
<p>The super-calculator shows the result of using different operators</p>
</header>
<div class="input-area">
<h2>Step 1: Enter the 1st Operand</h2>
<input type="text" id="operand1" value="1" />
<select id="operand1-type">
<option value="number">number</option>
<option value="string">string</option>
</select>
</div>
<div class="input-area">
<h2>Step 2: Choose an Operator</h2>
<div class="operator-choices">
<h3>Arithmetic Operators</h3>
<input type="radio" name="operator" value="+" checked>+
<input type="radio" name="operator" value="-">-
<input type="radio" name="operator" value="*">*
<input type="radio" name="operator" value="/">/
<input type="radio" name="operator" value="%">%
<h3>String Operator</h3>
<input type="radio" name="operator" value="concat">+ (concatenation)
<h3>Comparison Operators</h3>
<input type="radio" name="operator" value="==">==
<input type="radio" name="operator" value="===">===
<input type="radio" name="operator" value="!=">!=
<input type="radio" name="operator" value="!==">!==
<input type="radio" name="operator" value=">">>
<input type="radio" name="operator" value=">=">>=
<input type="radio" name="operator" value="<"><
<input type="radio" name="operator" value="<="><=
</div>
</div>
<div class="input-area">
<h2>Step 3: Enter the 2nd Operand</h2>
<input type="text" id="operand2" value="1">
<select id="operand2-type">
<option value="number">number</option>
<option value="string">string</option>
</select>
</div>
<div class="input-area">
<h2>Step 4: Click the Button</h2>
<input type="submit" id="submit" value="=">
</div>
<hr>
<div class="output-area">
<h2>Output</h2>
<h3>Operation</h3>
<span id="final-operation"></span>
<h3>Return Value:</h3>
<span id="result"></span>
</div>
</div>
<script>
//when the button is clicked
document.getElementById("submit").addEventListener("click",calculateIt);
function calculateIt() {
//create some variables
let myOperator;
let returnValue;
//get the operands
let operand1 = document.getElementById("operand1").value;
let operand2 = document.getElementById("operand2").value;
//get the operand types
let select1 = document.getElementById("operand1-type");
let select2 = document.getElementById("operand2-type");
let operand1type = select1.value;
let operand2type = select2.value;
//get the operator
let radios = document.getElementsByName('operator');
//convert the operands
switch (operand1type) {
case "string":
operand1 = String(operand1);
break;
case "number":
operand1 = Number(operand1);
break;
}
switch (operand2type) {
case "string":
operand2 = String(operand2);
break;
case "number":
operand2 = Number(operand2);
break;
}
//loop through each possible operand value and find the checked one
for (let i = 0, length = radios.length; i < length; i++) {
if (radios[i].checked) {
myOperator = radios[i].value;
//do a different operation depending on which operator was selected
switch (radios[i].value) {
case "+":
returnValue = operand1 + operand2;
break;
case "-":
returnValue = operand1 - operand2;
break;
case "*":
returnValue = operand1 * operand2;
break;
case "/":
returnValue = operand1 / operand2;
break;
case "%":
returnValue = operand1 % operand2;
break;
case "concat":
returnValue = `${operand1} ${operand2}`;
myOperator = "+";
break;
case "==":
returnValue = operand1 == operand2;
break;
case "===":
returnValue = operand1 === operand2;
break;
case "!=":
returnValue = operand1 != operand2;
break;
case "!==":
returnValue = operand1 !== operand2;
break;
case ">":
returnValue = operand1 > operand2;
break;
case ">=":
returnValue = operand1 >= operand2;
break;
case "<":
returnValue = operand1 < operand2;
break;
case "<=":
returnValue = operand1 <= operand2;
break;
}
break;
}
}
//display the operation
if (typeof(operand1)==="string"){
operand1 = ` ${operand1} `;
}
if (typeof(operand2)==="string"){
operand2 = ` ${operand2} `;
}
document.getElementById("final-operation").innerHTML = operand1 + myOperator + operand2;
//display the return value
document.getElementById("result").innerHTML = returnValue;
};
</script>
</body>
</html>