-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUpdate2.html
155 lines (145 loc) · 7.47 KB
/
Update2.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
<!DOCTYPE html>
<html lang="eng-us">
<title>Calculator</title>
<head>
<h1>This is a Calculator</h1>
</head>
<script>
function isNumeric(val) {
return /^-?\d+$/.test(val);
}
function Solve()
{
var isequation = true
const equation = Object.assign([], document.getElementById("equation").value);
//If there are numbers
if (document.getElementById("equation").value.includes("0") || document.getElementById("equation").value.includes("1") || document.getElementById("equation").value.includes("2") || document.getElementById("equation").value.includes("3") || document.getElementById("equation").value.includes("4") || document.getElementById("equation").value.includes("5") || document.getElementById("equation").value.includes("6") || document.getElementById("equation").value.includes("7") || document.getElementById("equation").value.includes("8") || document.getElementById("equation").value.includes("9"))
{
if( (document.getElementById("equation").value.includes("+") && document.getElementById("equation").value.includes("=")) || (document.getElementById("equation").value.includes("-") && document.getElementById("equation").value.includes("=")) || (document.getElementById("equation").value.includes("*") && document.getElementById("equation").value.includes("=")) || (document.getElementById("equation").value.includes("x") && document.getElementById("equation").value.includes("=")) || (document.getElementById("equation").value.includes("X") && document.getElementById("equation").value.includes("=")) || (document.getElementById("equation").value.includes("/") && document.getElementById("equation").value.includes("=")) || (document.getElementById("equation").value.includes("^") && document.getElementById("equation").value.includes("=")))
{
}
else
{
isequation = false;
}
for (let count = 0; count < equation.length; count++)
{
if((equation[count] == "+") || (equation[count] == "-") || (equation[count] == "*") || (equation[count] == "x") || (equation[count] == "X") || (equation[count] == "/"))
{
if((equation[count+1] == "+") || (equation[count+1] == "-") || (equation[count+1] == "*") || (equation[count+1] == "x") || (equation[count+1] == "X") || (equation[count+1] == "/"))
{
isequation = false;
}
}
}
if (equation[equation.length-1] != "=")
{
isequation = false;
}
}
else
{
isequation = false;
}
if (isequation == true)
{
const equation = Object.assign([], document.getElementById("equation").value);
//saves the numbers and equations
let number = ""
const numbers = []
const simbles = []
//0=*/,2=+-
let symbleon = 0
let finalnumber = 0
//pemdas
for (let count = 0; count < equation.length; count++)
{
if ((equation[count] == "0") || (equation[count] == "1") || (equation[count] == "2") || (equation[count] == "3") || (equation[count] == "4")|| (equation[count] == "5")|| (equation[count] == "6")|| (equation[count] == "7")|| (equation[count] == "8")|| (equation[count] == "9"))
{
number+=equation[count];
}
else
{
numbers.push(number);
number = "";
simbles.push(equation[count]);
}
};
for (let symblecount = 0; symblecount < simbles.length; symblecount++)
{
if (simbles[symblecount] == "^")
{
let numberthing = parseInt(numbers[symblecount])
for (let topnum = 0; topnum < parseInt(numbers[symblecount+1]); topnum++)
{
numberthing = numberthing*parseInt(numbers[symblecount]);
}
numbers[symblecount] = numberthing.toString();
numbers.splice(symblecount+1);
}
}
for (let symblecount = 0; symblecount < simbles.length; symblecount++)
{
if ((simbles[symblecount] == "/") || (simbles[symblecount] == "*") || (simbles[symblecount] == "x") || (simbles[symblecount] == "X"))
{
if (simbles[symblecount] == "/")
{
numbers[symblecount] = (parseInt(numbers[symblecount])/parseInt(numbers[symblecount+1])).toString();
numbers.splice(symblecount+1);
}
else
{
numbers[symblecount] = (parseInt(numbers[symblecount])*parseInt(numbers[symblecount+1])).toString();
numbers.splice(symblecount+1);
}
}
}
for (let symblecount = 0; symblecount < simbles.length; symblecount++)
{
if ((simbles[symblecount] == "+") || (simbles[symblecount] == "-"))
{
if (simbles[symblecount] == "+")
{
numbers[symblecount] = (parseInt(numbers[symblecount])+parseInt(numbers[symblecount+1])).toString();
numbers.splice(symblecount+1);
}
else
{
numbers[symblecount] = (parseInt(numbers[symblecount])-parseInt(numbers[symblecount+1])).toString();
numbers.splice(symblecount+1);
}
}
}
if (numbers[0] != "NaN")
{
document.getElementById("equation").value = document.getElementById("equation").value + numbers[0];
}
}
}
</script>
<body>
<div>
<a id="test">The Equation needs at least 2 numbers. It can only operate ^,+,-,* and /. The Equation must have = at the end of the equation. Can Only Operate on Positive numbers.</a>
<textarea id="equation" cols="40" rows="5">3+2*2=</textarea>
<button onclick="Solve()">Solve</button>
</div>
</body>
<style>
h1 {text-align: center;
font-size: xx-large;}
div {background-color: rgba(161, 161, 255, 0.336);
width: 100%;
height: 70vh;
min-height: fit-content;}
a {text-align: center;
width: 100%;
font-size: x-large;}
button {width: 100%;
font-size: xx-large;}
textarea {
height: 100%;
width: 100%;
font-size: xx-large;
}
</style>
</html>