Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
YorLecaros wants to merge3 commits intoTheAlgorithms:master
base:master
Choose a base branch
Loading
fromYorLecaros:feature/QuadraticFormula
Open
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

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 deletionsDIRECTORY.md
View file
Open in desktop
Original file line numberDiff line numberDiff 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
32 changes: 32 additions & 0 deletionsMaths/QuadraticFormula.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
/**
* @function quadraticFormula
* @description This script will find the the roots of a quadratic equation.
* @param {number} a
* @param {number} b
* @param {number} c
* @return {array}
* @see https://en.wikipedia.org/wiki/Quadratic_formula
* @example quadraticFormula(1, -3, -4) = [4, -1]
* @example quadraticFormula(1, 5, 6) = [-2, -3]
* @example quadraticFormula(1, -3, 8) = []
*/

const solveQuadraticEquation = (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 = [x1, x2]
} else if (discriminant === 0) {
const x = (-b) / denominator
answer = [x]
}
return answer
}

export { solveQuadraticEquation }
9 changes: 9 additions & 0 deletionsMaths/test/QuadraticFormula.test.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
import { solveQuadraticEquation } from '../QuadraticFormula'

test('Quadratic Equation', () => {
expect(solveQuadraticEquation(1, -3, -4)).toStrictEqual([4, -1])
expect(solveQuadraticEquation(1, 5, 6)).toStrictEqual([-2, -3])
expect(solveQuadraticEquation(3, 24, 48)).toStrictEqual([-4])
expect(solveQuadraticEquation(1, -3, 8)).toStrictEqual([])
expect(solveQuadraticEquation(1, -2, 9)).toStrictEqual([])
})
Loading

[8]ページ先頭

©2009-2025 Movatter.jp