-
Notifications
You must be signed in to change notification settings - Fork 1
/
CalcPart2.html
128 lines (121 loc) · 5.86 KB
/
CalcPart2.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
<!DOCTYPE html>
<html lang="eng-us">
<title>Calculator</title>
<head>
<h1>This is a Calculator</h1>
</head>
<script>
function Solve()
{
var isequation = true
//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("test").innerHTML = "g";
}
else
{
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 = []
let finalnumber = 0
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 count = 0; count < simbles.length+1; count++)
{
if (count==0)
{
if (simbles[count] == "+")
{
finalnumber = parseInt(numbers[0]) + parseInt(numbers[1]);
numbers.shift();
numbers.shift();
}
else if (simbles[count] == "-")
{
finalnumber = parseInt(numbers[0]) - parseInt(numbers[1]);
}
else if (simbles[count] == "x" || simbles[count] == "X" || simbles[count] == "*")
{
finalnumber = parseInt(numbers[0]) * parseInt(numbers[1]);
numbers.shift();
numbers.shift();
}
else if (simbles[count] == "/")
{
finalnumber = parseInt(numbers[0]) / parseInt(numbers[1]);
}
}
else
{
if (simbles[count] == "+")
{
finalnumber = finalnumber + parseInt(numbers[0]);
numbers.shift();
}
else if (simbles[count] == "x" || simbles[count] == "X" || simbles[count] == "*")
{
finalnumber = finalnumber * parseInt(numbers[0]);
numbers.shift();
}
else if (simbles[count] == "/")
{
finalnumber = finalnumber / parseInt(numbers[0]);
numbers.shift();
}
}
}
document.getElementById("test").innerHTML = finalnumber;
}
}
</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</a>
<textarea id="equation" cols="40" rows="5">2+3*10=</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>