From 174e2da47dd615542c878726800557c2c2269ea1 Mon Sep 17 00:00:00 2001 From: Dibya Date: Sat, 30 Sep 2023 22:37:32 +0530 Subject: [PATCH 1/8] Added QuadraticRoots in the Math/QuadraticRoots --- Maths/QuadraticRoots.js | 40 +++++++++++++++++++++++++++++++ Maths/test/QuadraticRoots.test.js | 19 +++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 Maths/QuadraticRoots.js create mode 100644 Maths/test/QuadraticRoots.test.js diff --git a/Maths/QuadraticRoots.js b/Maths/QuadraticRoots.js new file mode 100644 index 0000000000..0ffad4ca54 --- /dev/null +++ b/Maths/QuadraticRoots.js @@ -0,0 +1,40 @@ +/** + * + * Author - Dibya Debayan Dash + * Date: - 30/09/2023 + * Calculates the roots of a quadratic equation of the form ax^2 + bx + c = 0. + * + * @param {number} a - Coefficient of x^2. + * @param {number} b - Coefficient of x. + * @param {number} c - Constant term. + * @returns {number[]|string} - An array containing the roots if they are real, + * or a string indicating no real roots. + */ + +const quadraticRoots = (a, b, c) => { + // Calculate the discriminant + var discriminant = b * b - 4 * a * c + + // Check if roots are real + if (discriminant < 0) { + return 'No real roots' + } else if (discriminant === 0) { + // One real root + var root = -b / (2 * a) + return [root] + } else { + // Two real roots + var sqrtDiscriminant = Math.sqrt(discriminant) + var root1 = (-b + sqrtDiscriminant) / (2 * a) + var root2 = (-b - sqrtDiscriminant) / (2 * a) + return [root1, root2] + } +} +export { quadraticRoots } + +// Example usage +// var a = 1 +// var b = -3 +// var c = 2 + +// var roots = quadraticRoots(a, b, c) [2,1] diff --git a/Maths/test/QuadraticRoots.test.js b/Maths/test/QuadraticRoots.test.js new file mode 100644 index 0000000000..f76c6027e2 --- /dev/null +++ b/Maths/test/QuadraticRoots.test.js @@ -0,0 +1,19 @@ +import { quadraticRoots } from '../QuadraticRoots.js' + +// Test case 1: Two real roots +test('should return an array with two real roots when the discriminant is positive', () => { + const roots = quadraticRoots(1, -3, 2) + expect(roots).toEqual([2, 1]) +}) + +// Test case 2: One real root +test('should return an array with one real root when the discriminant is zero', () => { + const roots = quadraticRoots(1, -2, 1) + expect(roots).toEqual([1]) +}) + +// Test case 3: No real roots +test('should return a message indicating no real roots when the discriminant is negative', () => { + const roots = quadraticRoots(1, 2, 5) + expect(roots).toEqual('No real roots') +}) From e247d4382821da181cc7d868bae2b4b4704d0d56 Mon Sep 17 00:00:00 2001 From: Dibya Date: Sat, 30 Sep 2023 22:49:22 +0530 Subject: [PATCH 2/8] Fixed math/QyadraticRoots var to let --- Maths/QuadraticRoots.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/Maths/QuadraticRoots.js b/Maths/QuadraticRoots.js index 0ffad4ca54..ac6e155e0d 100644 --- a/Maths/QuadraticRoots.js +++ b/Maths/QuadraticRoots.js @@ -13,28 +13,28 @@ const quadraticRoots = (a, b, c) => { // Calculate the discriminant - var discriminant = b * b - 4 * a * c + let discriminant = b * b - 4 * a * c // Check if roots are real if (discriminant < 0) { return 'No real roots' } else if (discriminant === 0) { // One real root - var root = -b / (2 * a) + let root = -b / (2 * a) return [root] } else { // Two real roots - var sqrtDiscriminant = Math.sqrt(discriminant) - var root1 = (-b + sqrtDiscriminant) / (2 * a) - var root2 = (-b - sqrtDiscriminant) / (2 * a) + let sqrtDiscriminant = Math.sqrt(discriminant) + let root1 = (-b + sqrtDiscriminant) / (2 * a) + let root2 = (-b - sqrtDiscriminant) / (2 * a) return [root1, root2] } } export { quadraticRoots } // Example usage -// var a = 1 -// var b = -3 -// var c = 2 +// let a = 1 +// let b = -3 +// let c = 2 -// var roots = quadraticRoots(a, b, c) [2,1] +// let roots = quadraticRoots(a, b, c) [2,1] From cc965eb63757fb3cf53b090f4ce0de477d344cf6 Mon Sep 17 00:00:00 2001 From: Dibya Date: Sat, 30 Sep 2023 22:59:18 +0530 Subject: [PATCH 3/8] Added relevant links math/QyadraticRoots --- Maths/QuadraticRoots.js | 1 + 1 file changed, 1 insertion(+) diff --git a/Maths/QuadraticRoots.js b/Maths/QuadraticRoots.js index ac6e155e0d..0592774a1e 100644 --- a/Maths/QuadraticRoots.js +++ b/Maths/QuadraticRoots.js @@ -3,6 +3,7 @@ * Author - Dibya Debayan Dash * Date: - 30/09/2023 * Calculates the roots of a quadratic equation of the form ax^2 + bx + c = 0. + * link: https://www.cuemath.com/algebra/roots-of-quadratic-equation/ * * @param {number} a - Coefficient of x^2. * @param {number} b - Coefficient of x. From 057bc6435119e689d36e2d893007fc39a28b1645 Mon Sep 17 00:00:00 2001 From: Dibya Date: Sat, 30 Sep 2023 23:06:13 +0530 Subject: [PATCH 4/8] Added relevant links math/QyadraticRoots and fixed let - const --- Maths/QuadraticRoots.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Maths/QuadraticRoots.js b/Maths/QuadraticRoots.js index 0592774a1e..cf77ea1c86 100644 --- a/Maths/QuadraticRoots.js +++ b/Maths/QuadraticRoots.js @@ -14,20 +14,20 @@ const quadraticRoots = (a, b, c) => { // Calculate the discriminant - let discriminant = b * b - 4 * a * c + const discriminant = b * b - 4 * a * c // Check if roots are real if (discriminant < 0) { return 'No real roots' } else if (discriminant === 0) { // One real root - let root = -b / (2 * a) + const root = -b / (2 * a) return [root] } else { // Two real roots - let sqrtDiscriminant = Math.sqrt(discriminant) - let root1 = (-b + sqrtDiscriminant) / (2 * a) - let root2 = (-b - sqrtDiscriminant) / (2 * a) + const sqrtDiscriminant = Math.sqrt(discriminant) + const root1 = (-b + sqrtDiscriminant) / (2 * a) + const root2 = (-b - sqrtDiscriminant) / (2 * a) return [root1, root2] } } From e883c7af278001618e155df62553cc208ce9d8b3 Mon Sep 17 00:00:00 2001 From: Dibya Date: Sun, 1 Oct 2023 21:45:39 +0530 Subject: [PATCH 5/8] Added the changes and @see notation in Math/QuadraticRoots.js --- Maths/QuadraticRoots.js | 30 +++++++++++++----------------- Maths/test/QuadraticRoots.test.js | 15 ++++++--------- 2 files changed, 19 insertions(+), 26 deletions(-) diff --git a/Maths/QuadraticRoots.js b/Maths/QuadraticRoots.js index cf77ea1c86..5bd7b0b12a 100644 --- a/Maths/QuadraticRoots.js +++ b/Maths/QuadraticRoots.js @@ -1,17 +1,19 @@ /** - * - * Author - Dibya Debayan Dash - * Date: - 30/09/2023 + * @see https://www.cuemath.com/algebra/roots-of-quadratic-equation/ + * @author Dibya Debayan Dash * Calculates the roots of a quadratic equation of the form ax^2 + bx + c = 0. - * link: https://www.cuemath.com/algebra/roots-of-quadratic-equation/ * * @param {number} a - Coefficient of x^2. * @param {number} b - Coefficient of x. * @param {number} c - Constant term. * @returns {number[]|string} - An array containing the roots if they are real, * or a string indicating no real roots. + * + * @example + * // Find the roots of the quadratic equation: 2x^2 - 4x + 2 = 0 + * const roots = quadraticRoots(2, -4, 2); + * // Expected output: [1, 1] */ - const quadraticRoots = (a, b, c) => { // Calculate the discriminant const discriminant = b * b - 4 * a * c @@ -21,21 +23,15 @@ const quadraticRoots = (a, b, c) => { return 'No real roots' } else if (discriminant === 0) { // One real root - const root = -b / (2 * a) - return [root] + return [-b / (2 * a)] } else { // Two real roots const sqrtDiscriminant = Math.sqrt(discriminant) - const root1 = (-b + sqrtDiscriminant) / (2 * a) - const root2 = (-b - sqrtDiscriminant) / (2 * a) - return [root1, root2] + return [ + (-b + sqrtDiscriminant) / (2 * a), + (-b - sqrtDiscriminant) / (2 * a) + ] } } -export { quadraticRoots } - -// Example usage -// let a = 1 -// let b = -3 -// let c = 2 -// let roots = quadraticRoots(a, b, c) [2,1] +export { quadraticRoots } diff --git a/Maths/test/QuadraticRoots.test.js b/Maths/test/QuadraticRoots.test.js index f76c6027e2..e3f9edba7f 100644 --- a/Maths/test/QuadraticRoots.test.js +++ b/Maths/test/QuadraticRoots.test.js @@ -1,19 +1,16 @@ import { quadraticRoots } from '../QuadraticRoots.js' // Test case 1: Two real roots -test('should return an array with two real roots when the discriminant is positive', () => { - const roots = quadraticRoots(1, -3, 2) - expect(roots).toEqual([2, 1]) +test('returns an array with two real roots when the discriminant is positive', () => { + expect(quadraticRoots(1, -3, 2)).toEqual([2, 1]) }) // Test case 2: One real root -test('should return an array with one real root when the discriminant is zero', () => { - const roots = quadraticRoots(1, -2, 1) - expect(roots).toEqual([1]) +test('returns an array with one real root when the discriminant is zero', () => { + expect(quadraticRoots(1, -2, 1)).toEqual([1]) }) // Test case 3: No real roots -test('should return a message indicating no real roots when the discriminant is negative', () => { - const roots = quadraticRoots(1, 2, 5) - expect(roots).toEqual('No real roots') +test('returns a message indicating no real roots when the discriminant is negative', () => { + expect(quadraticRoots(1, 2, 5)).toEqual('No real roots') }) From a8d3342371908b5b93c9f223aaf606724d82182e Mon Sep 17 00:00:00 2001 From: Dibya Date: Sun, 1 Oct 2023 21:57:40 +0530 Subject: [PATCH 6/8] Added the changes Math/QuadraticRoots.js and return an empty [] --- Maths/QuadraticRoots.js | 6 +++--- Maths/test/QuadraticRoots.test.js | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Maths/QuadraticRoots.js b/Maths/QuadraticRoots.js index 5bd7b0b12a..f1341654b3 100644 --- a/Maths/QuadraticRoots.js +++ b/Maths/QuadraticRoots.js @@ -6,8 +6,8 @@ * @param {number} a - Coefficient of x^2. * @param {number} b - Coefficient of x. * @param {number} c - Constant term. - * @returns {number[]|string} - An array containing the roots if they are real, - * or a string indicating no real roots. + * @returns {number[]} - An array containing the roots if they are real, + * or an empty array indicating no real roots. * * @example * // Find the roots of the quadratic equation: 2x^2 - 4x + 2 = 0 @@ -20,7 +20,7 @@ const quadraticRoots = (a, b, c) => { // Check if roots are real if (discriminant < 0) { - return 'No real roots' + return [] } else if (discriminant === 0) { // One real root return [-b / (2 * a)] diff --git a/Maths/test/QuadraticRoots.test.js b/Maths/test/QuadraticRoots.test.js index e3f9edba7f..3a70128d90 100644 --- a/Maths/test/QuadraticRoots.test.js +++ b/Maths/test/QuadraticRoots.test.js @@ -11,6 +11,6 @@ test('returns an array with one real root when the discriminant is zero', () => }) // Test case 3: No real roots -test('returns a message indicating no real roots when the discriminant is negative', () => { - expect(quadraticRoots(1, 2, 5)).toEqual('No real roots') +test('returns an empty array indicating no real roots when the discriminant is negative', () => { + expect(quadraticRoots(1, 2, 5)).toEqual([]) }) From ef24e59409e4f929d128e877edbf86835086a6f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20M=C3=BCller?= <34514239+appgurueu@users.noreply.github.com> Date: Sun, 1 Oct 2023 20:09:04 +0200 Subject: [PATCH 7/8] Readd describe block, remove redundant comments --- Maths/test/QuadraticRoots.test.js | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/Maths/test/QuadraticRoots.test.js b/Maths/test/QuadraticRoots.test.js index 3a70128d90..bd0db43c15 100644 --- a/Maths/test/QuadraticRoots.test.js +++ b/Maths/test/QuadraticRoots.test.js @@ -1,16 +1,13 @@ import { quadraticRoots } from '../QuadraticRoots.js' -// Test case 1: Two real roots -test('returns an array with two real roots when the discriminant is positive', () => { - expect(quadraticRoots(1, -3, 2)).toEqual([2, 1]) -}) - -// Test case 2: One real root -test('returns an array with one real root when the discriminant is zero', () => { - expect(quadraticRoots(1, -2, 1)).toEqual([1]) -}) - -// Test case 3: No real roots -test('returns an empty array indicating no real roots when the discriminant is negative', () => { - expect(quadraticRoots(1, 2, 5)).toEqual([]) +describe('quadratic roots', () => { + it('returns an array with two real roots when the discriminant is positive', () => { + expect(quadraticRoots(1, -3, 2)).toEqual([2, 1]) + }) + it('returns an array with one real root when the discriminant is zero', () => { + expect(quadraticRoots(1, -2, 1)).toEqual([1]) + }) + it('returns an empty array indicating no real roots when the discriminant is negative', () => { + expect(quadraticRoots(1, 2, 5)).toEqual([]) + }) }) From a3cd2f32fb0d49eec597970631bfb1a2c9faac67 Mon Sep 17 00:00:00 2001 From: Dibya Date: Mon, 2 Oct 2023 00:23:59 +0530 Subject: [PATCH 8/8] Changed [1,1] to [1] --- Maths/QuadraticRoots.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Maths/QuadraticRoots.js b/Maths/QuadraticRoots.js index f1341654b3..a6b52cf1a9 100644 --- a/Maths/QuadraticRoots.js +++ b/Maths/QuadraticRoots.js @@ -12,7 +12,7 @@ * @example * // Find the roots of the quadratic equation: 2x^2 - 4x + 2 = 0 * const roots = quadraticRoots(2, -4, 2); - * // Expected output: [1, 1] + * // Expected output: [1] */ const quadraticRoots = (a, b, c) => { // Calculate the discriminant