-
-
Notifications
You must be signed in to change notification settings - Fork 134
/
index.html
194 lines (180 loc) · 7.32 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Fraction Calulator</title>
<link rel="stylesheet" href="./style.css">
</head>
<body>
<!-- partial:index.partial.html -->
<html>
<head>
<HEAD>
<script type="text/javascript">
var n1, n2, d1, d2, An, Ad, Op;
var neg = 1;
function solve() {
//If all fields are numbers
if (!isNaN(document.calc.n1.value) && !isNaN(document.calc.d1.value) && !isNaN(document.calc.n2.value) && !isNaN(document.calc.d2.value)) {
//If no fields are blank
if (document.calc.n1.value != '' && document.calc.d1.value != '' && document.calc.n2.value != '' && document.calc.d2.value != '') {
//Set variables:
n1 = document.calc.n1.value;// Numerator 1
d1 = document.calc.d1.value;// Numerator 2
n2 = document.calc.n2.value;// Denominator 1
d2 = document.calc.d2.value;// Denominator 2
Op = document.calc.Op.value;// Operator
} else {
//If blank field
alert('Please fill-in all fields!');
}
} else {
//If field has non-number
alert('Please enter only Numbers into the fields!');
}
//Which Operation
switch (Op) {
case '+':
//add fractions using formula ((n1*d2)+(n2*d1)) over (d1*d2)
An = (n1 * d2) + (n2 * d1) //Answer Numerator
Ad = (d1 * d2) //Answer Denominator
if (document.calc.reduce.checked == 1) {
reduce();
} else {
display();
}
break
case '-':
//subtract fractions using formula ((n1*d2)-(n2*d1)) over (d1*d2)
An = (n1 * d2) - (n2 * d1)//Answer Numerator
Ad = (d1 * d2)//Answer Denominator
if (document.calc.reduce.checked == 1) {
reduce();
} else {
display();
}
break
case '*':
//multiply fractions using formula (n1*n2) over (d1*d2)
An = n1 * n2;//Answer Numerator
Ad = d1 * d2; //Answer Denominator
if (document.calc.reduce.checked == 1) {
reduce();
} else {
display();
}
break
case '/':
//divide fractions using formula (n1*d2) over (d1*n2)
An = n1 * d2;//Answer Numerator
Ad = d1 * n2;//Answer Denominator
if (document.calc.reduce.checked == 1) {
reduce();
} else {
display();
}
break
}
}
function reduce() {
neg = 1; //1 if positive, -1 if negative
//convert to strings
ng = An + '';
dg = Ad + ''
if (ng.indexOf('-') != -1) { //check to see if answer is negative.
neg = -1
}
if (dg.indexOf('-') != -1) {
neg = -1
}
if (ng.indexOf('-') != -1 && dg.indexOf('-') != -1) {//if both numerator and denominator are negative the answer is positive
neg = 1
}
var factorX //highest common factor
if (An == 0 || Ad == 0) {
factorX = 1;
return;
}
An = Math.abs(An);
Ad = Math.abs(Ad);
var factorX = 1;
//Find common factors of Numerator and Denominator
for (var x = 2; x <= Math.min(An, Ad); x++) {
var check1 = An / x;
if (check1 == Math.round(check1)) {
var check2 = Ad / x;
if (check2 == Math.round(check2)) {
factorX = x;
}
}
}
An = (An / factorX) * neg; //divide by highest common factor to reduce fraction then multiply by neg to make positive or negative
Ad = Ad / factorX; //divide by highest common factor to reduce fraction
display();
}
function display() {
//Display answer
document.calc.An.value = An;
document.calc.Ad.value = Ad;
}
</script>
<title>Calculator</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<div id="wrap">
<div id="body">
<center>
<h2 style="text-align: center;margin-left: 30%;">Calculator</h2>
<p style="margin-left: 30%;">Click On "=" to get the result.</p>
<hr style="height:4px;border-width:5px;background-color:#2b83a5;text-align:center;margin-left:4%;margin-right:-25%">
<table width="500" height="400" align="center" border="0" cellspacing="0" cellpadding="4" style="p", input, tr: color: white; background: -webkit-linear-gradient(#000000, #252525, #000000);
background: -o-linear-gradient(#000000, #252525, #000000);
background: -moz-linear-gradient(#000000, #252525, #000000);
background: linear-gradient(#000000, #252525, #000000);border:1px #000000 solid;">
<tr>
<td align="center" valign="middle" class="maintable">
<h2>Fraction Calculator</h2>
<form name="calc">
<table width="150" height="70" border="0" cellspacing="10" cellpadding="20">
<tr>
<td style="border-bottom:2px #000000 solid;">
<input type="text" size="2" name="n1" id="n1" tabindex="1">
</td>
<td rowspan="2" align="center" valign="middle">
<select name="Op" tabindex="3"style="height: 35px;width: 41px;">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">x</option>
<option value="/">÷</option>
</select>
</td>
<td style="border-bottom:2px #000000 solid;"><input type="text" size="" name="n2" id="n2"
tabindex="4"></td>
<td rowspan="2" align="center" valign="middle"><input type="button" value=" = " onClick="solve();"
tabindex="6" style="width: 40px;height: 30px;"></td>
<td style="border-bottom:2px #000000 solid;"><input type="text" size="2" name="An" id="An"
readonly="1"></td>
</tr>
<tr>
<td><input type="text" size="2" name="d1" id="d1" tabindex="2"></td>
<td><input type="text" size="2" name="d2" id="d2" tabindex="5"></td>
<td><input type="text" size="2" name="Ad" id="Ad" readonly="1"></td>
</tr>
</table>
<br><input type="checkbox" name="reduce" id="reduce" checked> <h3>Reduce</h3>
</form>
</td>
</tr>
</table>
<hr style="height:4px;border-width:5px;background-color:#2b83a5;text-align:center;margin-left:4%;margin-right:-25%">
<div id="footnav">
<script src="footnav.js"></script>
</div>
</div>
</div>
</body>
<html>
<!-- partial -->
<script src="./script.js"></script>
</body>
</html>