

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Finding roots of a quadratic equation – JavaScript
We are required to write a JavaScript function that takes in three numbers (representing the coefficient of quadratic term, coefficient of linear term and the constant respectively in a quadratic quadratic).
And we are required to find the roots, (if they are real roots) otherwise we have to return false. Let's write the code for this function
Example
Following is the code −
const coefficients = [3, 12, 2]; const findRoots = co => { const [a, b, c] = co; const discriminant = (b * b) - 4 * a * c; if(discriminant < 0){ // the roots are non-real roots return false; }; const d = Math.sqrt(discriminant); const x1 = (d - b) / (2 * a); const x2 = ((d + b) * -1) / (2 * a); return [x1, x2]; }; console.log(findRoots(coefficients));
Output
The output in the console −
[ -0.17425814164944628, -3.825741858350554 ]
- Related Questions & Answers
- Java program to find the roots of a quadratic equation
- C++ Program to Find All Roots of a Quadratic Equation
- Java Program to Find all Roots of a Quadratic Equation
- C program to find the Roots of Quadratic equation
- How to write a C program to find the roots of a quadratic equation?
- Finding all solutions of a Diophantine equation using JavaScript
- How to Solve Quadratic Equation using Python?
- Absolute difference between sum and product of roots of a quartic equation?
- Program to find number of solutions in Quadratic Equation in C++
- Computing zeroes (solutions) of a mathematical equation in JavaScript
- Compute the roots of a polynomial with given complex roots in Python
- Finding the length of a JavaScript object
- Finding trailing zeros of a factorial JavaScript
- Compute the roots of a Chebyshev series with given complex roots in Python
- Compute the roots of a Laguerre series with given complex roots in Python
Advertisements