From 39e3c835eba3b8125c56d0d2f0e42eca0bda3602 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Fern=C3=A1ndez=20Serrata?= <76864299+Rudxain@users.noreply.github.com> Date: Thu, 28 Jul 2022 12:35:50 -0400 Subject: [PATCH 01/10] Make `PerfectCube` more correct and readable --- Maths/PerfectCube.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Maths/PerfectCube.js b/Maths/PerfectCube.js index bbfc821e2a..1091cb25fb 100644 --- a/Maths/PerfectCube.js +++ b/Maths/PerfectCube.js @@ -1,9 +1,11 @@ /** * Author: dephraiim * License: GPL-3.0 or later - * */ -const perfectCube = (num) => Math.round(num ** (1 / 3)) ** 3 === num +// Infinity is an undefined cube, so return false for any sign. +// This uses `round` instead of `floor` or `trunc`, to guard against rounding errors. +// currently, the function doesn't support `BigInt`s +const perfectCube = (num) => Number.isFinite(num) && Math.round(Math.cbrt(num)) ** 3 === num export { perfectCube } From c788dcbbb32f5d99e6c7186538bbe2093ae31586 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Fern=C3=A1ndez=20Serrata?= <76864299+Rudxain@users.noreply.github.com> Date: Thu, 28 Jul 2022 12:44:17 -0400 Subject: [PATCH 02/10] Add negative and Infinity tests --- Maths/test/PerfectCube.test.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Maths/test/PerfectCube.test.js b/Maths/test/PerfectCube.test.js index ff4b74f8f8..f3c4f219e6 100644 --- a/Maths/test/PerfectCube.test.js +++ b/Maths/test/PerfectCube.test.js @@ -3,8 +3,10 @@ import { perfectCube } from '../PerfectCube' describe('PerfectCube', () => { it('should return true for a perfect cube', () => { expect(perfectCube(125)).toBeTruthy() + expect(perfectCube(-125)).toBeTruthy() }) it('should return false for a non perfect cube', () => { expect(perfectCube(100)).toBeFalsy() + expect(perfectCube(Infinity)).toBeFalsy() }) }) From b7743e14bee835d2f8ea90c4d3ec64ee08f2d122 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Fern=C3=A1ndez=20Serrata?= <76864299+Rudxain@users.noreply.github.com> Date: Thu, 28 Jul 2022 12:49:09 -0400 Subject: [PATCH 03/10] Fixed comment formatting --- Maths/PerfectCube.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Maths/PerfectCube.js b/Maths/PerfectCube.js index 1091cb25fb..dd29da9f67 100644 --- a/Maths/PerfectCube.js +++ b/Maths/PerfectCube.js @@ -1,11 +1,12 @@ /** * Author: dephraiim * License: GPL-3.0 or later + * + * Infinity is an undefined cube, so return false for any sign. + * This uses `round` instead of `floor` or `trunc`, to guard against rounding errors. + * currently, the function doesn't support `BigInt`s */ -// Infinity is an undefined cube, so return false for any sign. -// This uses `round` instead of `floor` or `trunc`, to guard against rounding errors. -// currently, the function doesn't support `BigInt`s const perfectCube = (num) => Number.isFinite(num) && Math.round(Math.cbrt(num)) ** 3 === num export { perfectCube } From e5b5cf67266e809cfaa83d7b65a9b437697accab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Fern=C3=A1ndez=20Serrata?= <76864299+Rudxain@users.noreply.github.com> Date: Thu, 28 Jul 2022 12:54:27 -0400 Subject: [PATCH 04/10] Realized BigInt support is unnecessary The algorithm would be exactly the same, so there's no added educational value --- Maths/PerfectCube.js | 1 - 1 file changed, 1 deletion(-) diff --git a/Maths/PerfectCube.js b/Maths/PerfectCube.js index dd29da9f67..0a4d8a321c 100644 --- a/Maths/PerfectCube.js +++ b/Maths/PerfectCube.js @@ -4,7 +4,6 @@ * * Infinity is an undefined cube, so return false for any sign. * This uses `round` instead of `floor` or `trunc`, to guard against rounding errors. - * currently, the function doesn't support `BigInt`s */ const perfectCube = (num) => Number.isFinite(num) && Math.round(Math.cbrt(num)) ** 3 === num From bbdf16fb700335153f948e3da35bbe81f1672952 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Fern=C3=A1ndez=20Serrata?= <76864299+Rudxain@users.noreply.github.com> Date: Thu, 28 Jul 2022 13:02:36 -0400 Subject: [PATCH 05/10] Update PerfectCube.js --- Maths/PerfectCube.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Maths/PerfectCube.js b/Maths/PerfectCube.js index 0a4d8a321c..69fa660787 100644 --- a/Maths/PerfectCube.js +++ b/Maths/PerfectCube.js @@ -1,11 +1,12 @@ /** * Author: dephraiim * License: GPL-3.0 or later - * - * Infinity is an undefined cube, so return false for any sign. - * This uses `round` instead of `floor` or `trunc`, to guard against rounding errors. + * + * The integer check acts as a guard clause, and avoids calculating the root if the input is invalid, + * it also ensures Infinity is treated as a non-cube. + * This uses `round` instead of `floor` or `trunc`, to guard against potential `cbrt` accuracy errors. */ -const perfectCube = (num) => Number.isFinite(num) && Math.round(Math.cbrt(num)) ** 3 === num +const perfectCube = (num) => Number.isInteger(num) && Math.round(Math.cbrt(num)) ** 3 === num export { perfectCube } From 03008e4ad01550c244101afda97b9203670c6cbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Fern=C3=A1ndez=20Serrata?= <76864299+Rudxain@users.noreply.github.com> Date: Thu, 28 Jul 2022 13:24:37 -0400 Subject: [PATCH 06/10] Remove space --- Maths/PerfectCube.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Maths/PerfectCube.js b/Maths/PerfectCube.js index 69fa660787..b1a9fe1397 100644 --- a/Maths/PerfectCube.js +++ b/Maths/PerfectCube.js @@ -3,7 +3,7 @@ * License: GPL-3.0 or later * * The integer check acts as a guard clause, and avoids calculating the root if the input is invalid, - * it also ensures Infinity is treated as a non-cube. + * it also ensures Infinity is treated as a non-cube. * This uses `round` instead of `floor` or `trunc`, to guard against potential `cbrt` accuracy errors. */ From c1210aaa7ccb913c482e46e2ba26930381cddc5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Fern=C3=A1ndez=20Serrata?= <76864299+Rudxain@users.noreply.github.com> Date: Thu, 4 Aug 2022 18:29:31 -0400 Subject: [PATCH 07/10] Update PerfectCube.js Now `isInt` is replaced by `isFinite`, because `isInt` is a redundant check --- Maths/PerfectCube.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Maths/PerfectCube.js b/Maths/PerfectCube.js index b1a9fe1397..e00ce4177b 100644 --- a/Maths/PerfectCube.js +++ b/Maths/PerfectCube.js @@ -2,11 +2,9 @@ * Author: dephraiim * License: GPL-3.0 or later * - * The integer check acts as a guard clause, and avoids calculating the root if the input is invalid, - * it also ensures Infinity is treated as a non-cube. * This uses `round` instead of `floor` or `trunc`, to guard against potential `cbrt` accuracy errors. */ -const perfectCube = (num) => Number.isInteger(num) && Math.round(Math.cbrt(num)) ** 3 === num +const perfectCube = (num) => Number.isFinite(num) && Math.round(Math.cbrt(num)) ** 3 === num export { perfectCube } From 2491d258791a5efe45ccbb08eeea1ab57fa6335e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Fern=C3=A1ndez=20Serrata?= <76864299+Rudxain@users.noreply.github.com> Date: Thu, 4 Aug 2022 18:30:14 -0400 Subject: [PATCH 08/10] Update PerfectCube.js Remove dot --- Maths/PerfectCube.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Maths/PerfectCube.js b/Maths/PerfectCube.js index e00ce4177b..67e119305f 100644 --- a/Maths/PerfectCube.js +++ b/Maths/PerfectCube.js @@ -2,7 +2,7 @@ * Author: dephraiim * License: GPL-3.0 or later * - * This uses `round` instead of `floor` or `trunc`, to guard against potential `cbrt` accuracy errors. + * This uses `round` instead of `floor` or `trunc`, to guard against potential `cbrt` accuracy errors */ const perfectCube = (num) => Number.isFinite(num) && Math.round(Math.cbrt(num)) ** 3 === num From 7d0e2db74723c21acc8b64761ce8b418bfb6d18f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Fern=C3=A1ndez=20Serrata?= <76864299+Rudxain@users.noreply.github.com> Date: Thu, 4 Aug 2022 18:32:55 -0400 Subject: [PATCH 09/10] Parity between `PerfectSquare` and `PerfectCube` --- Maths/PerfectSquare.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Maths/PerfectSquare.js b/Maths/PerfectSquare.js index d4909dbd4f..9003ec1416 100644 --- a/Maths/PerfectSquare.js +++ b/Maths/PerfectSquare.js @@ -2,8 +2,9 @@ * Author: dephraiim * License: GPL-3.0 or later * + * This uses `round` instead of `floor` or `trunc`, to guard against potential `sqrt` accuracy errors */ -const perfectSquare = (num) => Math.sqrt(num) ** 2 === num +const perfectSquare = (num) => Number.isFinite(num) && Math.round(Math.sqrt(num)) ** 2 === num export { perfectSquare } From cc4e44eb5ad39917e6a835bffadb2a1487d33008 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Fern=C3=A1ndez=20Serrata?= <76864299+Rudxain@users.noreply.github.com> Date: Thu, 4 Aug 2022 18:37:33 -0400 Subject: [PATCH 10/10] Update PerfectSquare.test.js Fixed the old copy-paste error that said "cube" instead of "square". And added `Infinity` test --- Maths/test/PerfectSquare.test.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Maths/test/PerfectSquare.test.js b/Maths/test/PerfectSquare.test.js index 86c8c34033..37fce248d4 100644 --- a/Maths/test/PerfectSquare.test.js +++ b/Maths/test/PerfectSquare.test.js @@ -1,10 +1,11 @@ import { perfectSquare } from '../PerfectSquare' describe('PerfectSquare', () => { - it('should return true for a perfect cube', () => { + it('should return true for a perfect square', () => { expect(perfectSquare(16)).toBeTruthy() }) - it('should return false for a non perfect cube', () => { + it('should return false for a non perfect square', () => { expect(perfectSquare(10)).toBeFalsy() + expect(perfectSquare(Infinity)).toBeFalsy() }) })