JavaScript Program to Solve Quadratic Equation (4 Ways)
Solving a quadratic equation is a fundamental mathematical task that can be solved efficiently using JavaScript. A quadratic equation is expressed in the standard form:
ax² + bx + c = 0
where a, b, and c are constants, and x represents the unknown variable. The solutions (roots) can be found using the quadratic formula:
x = (-b ± √(b² - 4ac)) / 2a
In this guide, we will explore different methods to write a JavaScript program to solve quadratic equations, complete with examples, explanations, and outputs.
JavaScript Program to Solve Quadratic Equation Using Hardcoded Values
The simplest approach is to use predefined values for a, b, and c in the program.
Code
let a = 1;
let b = -3;
let c = 2;
let discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
let root1 = (-b + Math.sqrt(discriminant)) / (2 * a);
let root2 = (-b - Math.sqrt(discriminant)) / (2 * a);
console.log(`The roots are real and distinct: ${root1}, ${root2}`);
} else if (discriminant === 0) {
let root = -b / (2 * a);
console.log(`The roots are real and equal: ${root}`);
} else {
console.log("The roots are complex and cannot be solved using real numbers.");
}
Output
The roots are real and distinct: 2, 1
Explanation
The discriminant (b² - 4ac) determines the nature of the roots.
- If discriminant > 0, the roots are real and distinct.
- If discriminant === 0, the roots are real and equal.
- If discriminant < 0, the roots are complex.
JavaScript Program to Solve Quadratic Equation Using User Input
This method makes the program interactive by allowing users to input the values of a, b, and c.
Code
let a = parseFloat(prompt("Enter the value of a:"));
let b = parseFloat(prompt("Enter the value of b:"));
let c = parseFloat(prompt("Enter the value of c:"));
let discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
let root1 = (-b + Math.sqrt(discriminant)) / (2 * a);
let root2 = (-b - Math.sqrt(discriminant)) / (2 * a);
alert(`The roots are real and distinct: ${root1}, ${root2}`);
} else if (discriminant === 0) {
let root = -b / (2 * a);
alert(`The roots are real and equal: ${root}`);
} else {
alert("The roots are complex and cannot be solved using real numbers.");
}
Output
The program prompts the user to input values and shows the results in an alert box.
Explanation
-
The program uses prompt() to get user input for a, b, and c.
-
Based on the discriminant, it calculates the roots and displays them using alert().
JavaScript Program to Solve Quadratic Equation Using Functions for Reusability
Creating a reusable function to solve quadratic equations makes the program more modular and clean.
Code
function solveQuadratic(a, b, c) {
let discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
let root1 = (-b + Math.sqrt(discriminant)) / (2 * a);
let root2 = (-b - Math.sqrt(discriminant)) / (2 * a);
return `The roots are real and distinct: ${root1}, ${root2}`;
} else if (discriminant === 0) {
let root = -b / (2 * a);
return `The roots are real and equal: ${root}`;
} else {
return "The roots are complex and cannot be solved using real numbers.";
}
}
let result = solveQuadratic(1, -3, 2);
console.log(result);
Output
The roots are real and distinct: 2, 1
Explanation
The solveQuadratic function encapsulates the logic for solving quadratic equations, making it reusable for multiple inputs.
JavaScript Program to Solve Quadratic Equation Using HTML for Interactive Input
This method uses an HTML form to allow users to input values and see the results dynamically.
Code
<!DOCTYPE html>
<html>
<body>
<h3>Solve Quadratic Equation</h3>
<form>
<label for="a">a:</label>
<input type="text" id="a"><br><br>
<label for="b">b:</label>
<input type="text" id="b"><br><br>
<label for="c">c:</label>
<input type="text" id="c"><br><br>
<button type="button" onclick="solveQuadratic()">Solve</button>
</form>
<p id="result"></p>
<script>
function solveQuadratic() {
let a = parseFloat(document.getElementById("a").value);
let b = parseFloat(document.getElementById("b").value);
let c = parseFloat(document.getElementById("c").value);
let discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
let root1 = (-b + Math.sqrt(discriminant)) / (2 * a);
let root2 = (-b - Math.sqrt(discriminant)) / (2 * a);
document.getElementById("result").innerText = `The roots are real and distinct: ${root1}, ${root2}`;
} else if (discriminant === 0) {
let root = -b / (2 * a);
document.getElementById("result").innerText = `The roots are real and equal: ${root}`;
} else {
document.getElementById("result").innerText = "The roots are complex and cannot be solved using real numbers.";
}
}
</script>
</body>
</html>
Output
The results are shown on the webpage after clicking the "Solve" button.
Explanation
The solveQuadratic function reads values from input fields, calculates the roots, and displays the results dynamically on the webpage.