Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@
* [PowLogarithmic](Maths/PowLogarithmic.js)
* [PrimeCheck](Maths/PrimeCheck.js)
* [PrimeFactors](Maths/PrimeFactors.js)
* [QuadraticFormula](Maths/QuadraticFormula.js)
* [RadianToDegree](Maths/RadianToDegree.js)
* [ReverseNumber](Maths/ReverseNumber.js)
* [ReversePolishNotation](Maths/ReversePolishNotation.js)
Expand Down
31 changes: 31 additions & 0 deletions Maths/QuadraticFormula.js
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) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please rename to solveQuadraticEquation

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, no problem 👍

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 = ''
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use strings; instead, return an array of solutions (empty array for no solutions, array with single item for one solution, two items for two solutions).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I'll do that...thanks!

if (discriminant >= 0) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please distinguish discriminant == 0 here to only add a single item to answer in that case.

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 }
21 changes: 21 additions & 0 deletions Maths/test/QuadraticFormula.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { quadraticFormula } from '../QuadraticFormula'

test('Testing on quadraticFormula', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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')
})
Loading