-
- Notifications
You must be signed in to change notification settings - Fork 5.8k
algorithm: quadratic formula #1151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* @function quadraticFormula | ||
* @description This script will find the the roots of a quadratic equation. | ||
* @param {number} a | ||
* @param {number} b | ||
* @param {number} c | ||
* @return {string} | ||
* @see https://en.wikipedia.org/wiki/Quadratic_formula | ||
* @example quadraticFormula(1, -3, -4) = 'the roots are X1=4 and X2=-1' | ||
* @example quadraticFormula(1, 5, 6) = 'the roots are X1=-2 and X2=-3' | ||
* @example quadraticFormula(1, -3, 8) = 'the roots do not exist or the roots are imaginary' | ||
*/ | ||
| ||
const quadraticFormula = (a, b, c) => { | ||
if (typeof a !== 'number' || typeof b !== 'number' || typeof c !== 'number') { | ||
return new TypeError('Some argument is not a number.') | ||
} | ||
const discriminant = (b ** 2) - (4 * a * c) | ||
const denominator = 2 * a | ||
let answer = '' | ||
| ||
if (discriminant >= 0) { | ||
| ||
const x1 = (-b + Math.sqrt(discriminant)) / denominator | ||
const x2 = (-b - Math.sqrt(discriminant)) / denominator | ||
answer = `the roots are X1=${x1} and X2=${x2}` | ||
} else { | ||
answer = 'the roots do not exist or the roots are imaginary' | ||
} | ||
return answer | ||
} | ||
| ||
export { quadraticFormula } |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { quadraticFormula } from '../QuadraticFormula' | ||
| ||
test('Testing on quadraticFormula', () => { | ||
| ||
const quadraticForm = quadraticFormula(1, -3, -4) | ||
expect(quadraticForm).toBe('the roots are X1=4 and X2=-1') | ||
}) | ||
| ||
test('Testing on quadraticFormula', () => { | ||
const quadraticForm = quadraticFormula(1, 5, 6) | ||
expect(quadraticForm).toBe('the roots are X1=-2 and X2=-3') | ||
}) | ||
| ||
test('Testing on quadraticFormula', () => { | ||
const quadraticForm = quadraticFormula(1, -3, 8) | ||
expect(quadraticForm).toBe('the roots do not exist or the roots are imaginary') | ||
}) | ||
| ||
test('Testing on quadraticFormula', () => { | ||
const quadraticForm = quadraticFormula(1, -2, 9) | ||
expect(quadraticForm).toBe('the roots do not exist or the roots are imaginary') | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please rename to
solveQuadraticEquation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, no problem 👍