
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Computing zeroes (solutions) of a mathematical equation in 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.
Example
The code for this will be −
const coeff = [1, 12, 3]; const findRoots = co => { const [a, b, c] = co; const discriminant = (b * b) - 4 * a * c; // non real roots if(discriminant < 0){ 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(coeff));
Output
The output in the console −
[ -0.2554373534619714, -11.744562646538029 ]
- Related Articles
- Finding all solutions of a Diophantine equation using JavaScript
- How to plot a plane using some mathematical equation in matplotlib?
- Find number of solutions of a linear equation of n variables in C++
- Evaluating a string as a mathematical expression in JavaScript
- Number of non-negative integral solutions of sum equation in C++
- Program to find number of solutions in Quadratic Equation in C++
- Evaluating a mathematical expression considering Operator Precedence in JavaScript
- Find the number of solutions to the given equation in C++
- Removing parentheses from mathematical expressions in JavaScript
- Number of integral solutions of the equation x1 + x2 +…. + xN = k in C++
- What are basic JavaScript mathematical operators?
- Find the Number of solutions for the equation x + y + z
- Computing Ackerman number for inputs in JavaScript
- Finding roots of a quadratic equation – JavaScript
- Positive, negative and zeroes contribution of an array in JavaScript

Advertisements