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

Commit f271a2c

Browse files
Dibya12345chapati456appgurueu
authored
Added QuadraticRoots to Math/QuadraticRoots.js (#1376)
* Added QuadraticRoots in the Math/QuadraticRoots * Fixed math/QyadraticRoots var to let * Added relevant links math/QyadraticRoots * Added relevant links math/QyadraticRoots and fixed let - const * Added the changes and @see notation in Math/QuadraticRoots.js * Added the changes Math/QuadraticRoots.js and return an empty [] * Readd describe block, remove redundant comments * Changed [1,1] to [1] --------- Co-authored-by: Dibya <Dibya.DebayanDash@siriuscom.com> Co-authored-by: Lars Müller <34514239+appgurueu@users.noreply.github.com>
1 parent 7ff3e5e commit f271a2c

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

‎Maths/QuadraticRoots.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* @see https://www.cuemath.com/algebra/roots-of-quadratic-equation/
3+
* @author Dibya Debayan Dash
4+
* Calculates the roots of a quadratic equation of the form ax^2 + bx + c = 0.
5+
*
6+
* @param {number} a - Coefficient of x^2.
7+
* @param {number} b - Coefficient of x.
8+
* @param {number} c - Constant term.
9+
* @returns {number[]} - An array containing the roots if they are real,
10+
* or an empty array indicating no real roots.
11+
*
12+
* @example
13+
* // Find the roots of the quadratic equation: 2x^2 - 4x + 2 = 0
14+
* const roots = quadraticRoots(2, -4, 2);
15+
* // Expected output: [1]
16+
*/
17+
const quadraticRoots = (a, b, c) => {
18+
// Calculate the discriminant
19+
const discriminant = b * b - 4 * a * c
20+
21+
// Check if roots are real
22+
if (discriminant < 0) {
23+
return []
24+
} else if (discriminant === 0) {
25+
// One real root
26+
return [-b / (2 * a)]
27+
} else {
28+
// Two real roots
29+
const sqrtDiscriminant = Math.sqrt(discriminant)
30+
return [
31+
(-b + sqrtDiscriminant) / (2 * a),
32+
(-b - sqrtDiscriminant) / (2 * a)
33+
]
34+
}
35+
}
36+
37+
export { quadraticRoots }

‎Maths/test/QuadraticRoots.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { quadraticRoots } from '../QuadraticRoots.js'
2+
3+
describe('quadratic roots', () => {
4+
it('returns an array with two real roots when the discriminant is positive', () => {
5+
expect(quadraticRoots(1, -3, 2)).toEqual([2, 1])
6+
})
7+
it('returns an array with one real root when the discriminant is zero', () => {
8+
expect(quadraticRoots(1, -2, 1)).toEqual([1])
9+
})
10+
it('returns an empty array indicating no real roots when the discriminant is negative', () => {
11+
expect(quadraticRoots(1, 2, 5)).toEqual([])
12+
})
13+
})

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /