forked from 0xvashishth/CalcHub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
84 lines (71 loc) · 1.9 KB
/
app.js
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
//declaring variables
var a,
b,
c,
d = 0,
displayString = "",
root1 = 0,
root2 = 0;
// function to point out not-valid cases
function quadraticValue() {
a = document.getElementById("a").value;
b = document.getElementById("b").value;
c = document.getElementById("c").value;
//box not filled
if (a === "" || b === "" || c === "")
document.getElementById("e-text").innerHTML = "❌ Fill All 3 Boxes";
else {
//value of a is 0
if (a == 0)
document.getElementById("e-text").innerHTML =
"❌ Error coefficient of x<sup>2</sup> should be >0";
//no errors
else calculateRoot();
}
}
//function to display the determinant nature
function determinantValue() {
if (d > 0) {
displayString = "Both roots are Real & Distinct";
} else if (d < 0) {
displayString = "Both roots are Distinct & Imaginary";
} else if (d == 0) {
displayString = "Both roots are Real & Equal";
}
displayDet();
function displayDet() {
document.getElementById("e-text").innerHTML = displayString;
}
}
//function to calculate roots
function calculateRoot() {
d = b * b - 4 * a * c;
determinantValue();
//condition for real and different roots
if (d > 0) {
root1 = ((-b + Math.sqrt(d)) / (2 * a)).toFixed(2);
root2 = ((-b - Math.sqrt(d)) / (2 * a)).toFixed(2);
// result
displayRoot();
}
// condition for real and equal roots
else if (d == 0) {
root1 = root2 = (-b / (2 * a)).toFixed(2);
// result
displayRoot();
}
// if roots are not real
else {
let realPart = (-b / (2 * a)).toFixed(2);
let imagPart = (Math.sqrt(-d) / (2 * a)).toFixed(2);
// result
root1 = `${realPart} + ${imagPart}i`;
root2 = `${realPart} - ${imagPart}i`;
displayRoot();
}
}
//function to display the roots value
function displayRoot() {
document.getElementById("x1").innerHTML = root1;
document.getElementById("x2").innerHTML = root2;
}