-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcubic.html
48 lines (35 loc) · 1.69 KB
/
cubic.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
<!DOCTYPE html>
<html>
<head>
<title>Cubic Equation Calculator - Online</title>
</head>
<body>
<h1>Cubic Equation Calculator - Online</h1>
<p>A <b>X<sup>3</sup></b> + B <b>X<sup>2</sup></b> + C <b>X<sup>1</sup></b> + D = 0</p>
<form name="form" method="post" action="" onsubmit="test(form.a.value, form.b.value, form.c.value, form.d.value);return false;">
A: <input type="number" name="a" value="1" size="25" maxlength="25">
<br>B: <input type="number" name="b" value="2" size="25" maxlength="25">
<br>C: <input type="number" name="c" value="2" size="25" maxlength="25">
<br>D: <input type="number" name="d" value="2" size="25" maxlength="25">
<br><button>Calculate Cubic</button>
<h4>Output:</h4>
<div id="log"></div>
<mark><b id="roots"></b></mark>
</form>
<script src="cubic.js" type="text/javascript"></script>
<script type="text/javascript">
const elm_log = document.querySelector("#log")
const elm_roots = document.querySelector("#roots")
const test = (a, b, c, d) => {
const res = cubicSolve(a, b, c, d)
console.log(res)
const display = (value) => {
if(value.i === 0) return `${value.real}`
else return `${value.real} + ${value.i} <i>i</i>`
}
elm_roots.innerHTML = `X<sub>1</sub> = ${display(res[0])}<br>X<sub>2</sub> = ${display(res[1])}<br>X<sub>3</sub> = ${display(res[2])}<br>`
}
window.addEventListener("load", () => { document.querySelector("button").click() })
</script>
</body>
</html>