-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator.html
More file actions
85 lines (68 loc) · 3.11 KB
/
Copy pathcalculator.html
File metadata and controls
85 lines (68 loc) · 3.11 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Quadratic Equation Solver</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<script>
var a, b, c;
var outputText;
function validate() {
// get the input
a = document.forms["input_form"]["aterm"].value;
b = document.forms["input_form"]["bterm"].value;
c = document.forms["input_form"]["cterm"].value;
// validate a, b and c
if (a == 0) {
outputText = "<em>a</em> cannot equal zero!";
} else if (isNaN(a)) {
outputText = "<em>a</em> must be a number!";
} else if (isNaN(b)) {
outputText = "<em>b</em> must be a number!";
} else if (isNaN(c)) {
outputText = "<em>c</em> must be a number!";
} else {
// calculate the result using x = (-b +- sqrt(b^2 - 4ac)) / 2a
var x1 = (-b - Math.sqrt(Math.pow(b, 2) - 4 * a * c)) / (2 * a);
var x2 = (-b + Math.sqrt(Math.pow(b, 2) - 4 * a * c)) / (2 * a);
outputText = "For the equation <strong>" + (a == 1 ? "" : a) + "x\u00B2 + " + (b == 1 ? "" : b) + "x + " + c + " = 0</strong>, x can equal <strong>" + x1 + "</strong> or <strong>" + x2 + "</strong>";
}
// output the result (or errors)
document.getElementById("output_text").innerHTML = outputText;
}
</script>
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="BearyHome.html">Beary</a>
</div>
<ul class="nav navbar-nav">
<li class="active"><a href="NavigationBar.html">Home</a></li>
<li><a href="page2OFtheWebsite.html">Competitive Programming123</a></li>
<li><a href="calculator.html">calculator</a></li>
<li><a href="JavaBasics.html">Java Basics</a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a href="#"><span class="glyphicon glyphicon-user"></span> Sign Up</a></li>
<li><a href="#"><span class="glyphicon glyphicon-log-in"></span> Login</a></li>
</ul>
</div>
</nav>
</head>
<body>
<center>
<p>Please enter the coefficients <strong>a</strong>, <strong>b</strong> and <strong>c</strong> of a quadratic equation of the form <strong>ax² + bx + c = 0</strong></p>
<form name="input_form" action="javascript:validate();">
a: <input type="text" name="aterm" size="5" required>
b: <input type="text" name="bterm" size="5" required>
c: <input type="text" name="cterm" size="5" required>
<br><br>
<input type="submit" value="Calculate">
</form>
<p id="output_text"/>
</center>
</body>
</html>