- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 Articles
- C++ Program to Find All Roots of a Quadratic Equation
- Java program to find the roots of a quadratic equation
- Java Program to Find all Roots of a Quadratic Equation
- Kotlin Program to Find all Roots of a Quadratic Equation
- Haskell program to find all roots of a quadratic equation
- C program to find the Roots of Quadratic equation
- How to Find all Roots of a Quadratic Equation in Golang?
- How to write a C program to find the roots of a quadratic equation?
- A quadratic equation with integral coefficient has integral roots. Justify your answer.
- Finding all solutions of a Diophantine equation using JavaScript
- Find the quadratic roots in the equation$4x^{2}-3x+7$
- Write all the values of k for which the quadratic equation $x^2+kx+16=0$ has equal roots. Find the roots of the equation so obtained.
- Find $p$, if quadratic equation $py( y-2)+6=0$ has equal roots.
- If $1$ is a root of the quadratic equation $3x^2 + ax - 2 = 0$ and the quadratic equation $a(x^2 + 6x) - b = 0$ has equal roots, find the value of b.
- If $2$ is a root of the quadratic equation $3x^2 + px - 8 = 0$ and the quadratic equation $4x^2 - 2px + k = 0$ has equal roots, find the value of k.

Advertisements