JavaScript Program to Solve Quadratic Equation

Updated on November 18, 2024
Solve quadratic equation header image

Introduction

A quadratic equation is a second degree polynomial usually expressed as ( ax^2 + bx + c = 0 ). Solving these equations is foundational in algebra and crops up in various scientific and engineering applications. JavaScript, with its extensive capabilities, can easily handle the logic and computation involved in solving these equations.

In this article, you will learn how to solve quadratic equations using JavaScript. Through clear examples, explore how to implement the formula programmatically, handle different types of roots, and even manage edge cases where the quadratic equation might not fit the usual scenarios.

Implementing the Quadratic Formula

The Quadratic Formula, ( x = \frac{-b \pm \sqrt{b^2-4ac}}{2a} ), is used to find the values of ( x ) that satisfy the equation ( ax^2 + bx + c = 0 ). In JavaScript, utilize the Math library to handle the square root and other arithmetic operations.

Solve for Real Roots

  1. Define the coefficients ( a ), ( b ), and ( c ).

  2. Compute the discriminant ( \Delta = b^2 - 4ac ).

  3. Use conditional statements to check the discriminant and compute the roots.

    javascript
    function solveQuadratic(a, b, c) {
        let discriminant = b ** 2 - 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 different: ${root1}, ${root2}`;
        } else if (discriminant == 0) {
            let root = -b / (2 * a);
            return `The roots are real and same: ${root}`;
        } else {
            return "The roots are complex.";
        }
    }
    

    This function calculates the roots of the quadratic equation given the coefficients ( a ), ( b ), and ( c ). It then classifies the roots based on the discriminant’s value: positive for distinct real roots, zero for a repeated real root, and negative for complex roots.

Example Usage

  1. Use the solveQuadratic function with specific coefficients.

    javascript
    console.log(solveQuadratic(1, -3, 2));  // Outputs: The roots are real and different: 2, 1
    console.log(solveQuadratic(1, 2, 1));   // Outputs: The roots are real and same: -1
    console.log(solveQuadratic(1, 0, 1));   // Outputs: The roots are complex.
    

    Demonstrates how to use the function with different sets of coefficients to see the roots’ nature — real distinct, real same, or complex.

Handling Edge Cases

When ( a ) is Zero

  1. Verify if ( a ) is non-zero to ensure it's a valid quadratic equation.

  2. Provide feedback if ( a ) equals zero since it's not a quadratic equation.

    javascript
    function solveQuadratic(a, b, c) {
        if (a === 0) {
            return "This is not a quadratic equation.";
        }
        let discriminant = b ** 2 - 4 * a * c;
    
        // Root calculation logic...
    }
    

    If ( a ) is zero, the equation is linear, not quadratic. This snippet ensures that the function handles such cases gracefully.

Conclusion

Solving quadratic equations using JavaScript is straightforward with the proper application of the quadratic formula and JavaScript’s built-in Math functions. By handling various coefficients and discriminant scenarios, you can develop robust applications that deal with mathematical computations effectively. Utilize these examples to enhance your JavaScript projects or integrate them into educational tools that assist in learning algebra.