-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4.8.html
More file actions
64 lines (60 loc) · 2.55 KB
/
Copy path4.8.html
File metadata and controls
64 lines (60 loc) · 2.55 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
<!DOCTYPE html>
<html>
<body>
<input size="3" id="a" placeholder="a"> x<sup>2</sup> +
<input size="3" id="b" placeholder="b"> x +
<input size="3" id="c" placeholder="c"> = 0
<input type="button" value="Solve" onclick="solve()">
<div id="solution">Solution area</div>
<script>
function solve() {
let a = document.getElementById('a');
let b = document.getElementById('b');
let c = document.getElementById('c'); // Correct the reference for 'c'
let solutionArea = document.getElementById('solution');
solutionArea.innerHTML = ""; // Clear previous solutions
let err = '';
if (isNaN(a.value)) {
err = 'a is not a number.';
a.focus();
} else if (isNaN(b.value)) {
err = 'b is not a number.';
b.focus();
} else if (isNaN(c.value)) {
err = 'c is not a number.';
c.focus();
} else {
a = parseFloat(a.value);
b = parseFloat(b.value);
c = parseFloat(c.value);
if (a === 0) {
if (b === 0) {
if (c === 0) {
err = "Any x solves the equation.";
} else {
err = "No solution exists.";
}
} else {
err = "x = " + (-c / b);
}
} else {
let delta = b ** 2 - 4 * a * c;
if (delta > 0) {
let x1 = (-b + Math.sqrt(delta)) / (2 * a);
let x2 = (-b - Math.sqrt(delta)) / (2 * a);
err = "x1 = " + x1 + ", x2 = " + x2;
} else if (delta === 0) {
let x = -b / (2 * a);
err = "x = " + x;
} else {
let realPart = (-b / (2 * a));
let imaginaryPart = Math.sqrt(-delta) / (2 * a);
err = "x1 = " + realPart + " + " + imaginaryPart + "i, x2 = " + realPart + " - " + imaginaryPart + "i";
}
}
}
solutionArea.innerHTML = err; // Set computed result
}
</script>
</body>
</html>