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 9790468

Browse files
authored
merge: Edit Perfect{Square, Cube} (#1071)
* Make `PerfectCube` more correct and readable * Add negative and Infinity tests * Fixed comment formatting * Realized BigInt support is unnecessary The algorithm would be exactly the same, so there's no added educational value * Update PerfectCube.js * Remove space * Update PerfectCube.js Now `isInt` is replaced by `isFinite`, because `isInt` is a redundant check * Update PerfectCube.js Remove dot * Parity between `PerfectSquare` and `PerfectCube` * Update PerfectSquare.test.js Fixed the old copy-paste error that said "cube" instead of "square". And added `Infinity` test
1 parent e9b8b13 commit 9790468

File tree

4 files changed

+9
-4
lines changed

4 files changed

+9
-4
lines changed

‎Maths/PerfectCube.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
* Author: dephraiim
33
* License: GPL-3.0 or later
44
*
5+
* This uses `round` instead of `floor` or `trunc`, to guard against potential `cbrt` accuracy errors
56
*/
67

7-
const perfectCube = (num) => Math.round(num**(1/3)) ** 3 === num
8+
const perfectCube = (num) => Number.isFinite(num)&&Math.round(Math.cbrt(num)) ** 3 === num
89

910
export { perfectCube }

‎Maths/PerfectSquare.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
* Author: dephraiim
33
* License: GPL-3.0 or later
44
*
5+
* This uses `round` instead of `floor` or `trunc`, to guard against potential `sqrt` accuracy errors
56
*/
67

7-
const perfectSquare = (num) => Math.sqrt(num) ** 2 === num
8+
const perfectSquare = (num) => Number.isFinite(num)&&Math.round(Math.sqrt(num)) ** 2 === num
89

910
export { perfectSquare }

‎Maths/test/PerfectCube.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ import { perfectCube } from '../PerfectCube'
33
describe('PerfectCube', () => {
44
it('should return true for a perfect cube', () => {
55
expect(perfectCube(125)).toBeTruthy()
6+
expect(perfectCube(-125)).toBeTruthy()
67
})
78
it('should return false for a non perfect cube', () => {
89
expect(perfectCube(100)).toBeFalsy()
10+
expect(perfectCube(Infinity)).toBeFalsy()
911
})
1012
})

‎Maths/test/PerfectSquare.test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { perfectSquare } from '../PerfectSquare'
22

33
describe('PerfectSquare', () => {
4-
it('should return true for a perfect cube', () => {
4+
it('should return true for a perfect square', () => {
55
expect(perfectSquare(16)).toBeTruthy()
66
})
7-
it('should return false for a non perfect cube', () => {
7+
it('should return false for a non perfect square', () => {
88
expect(perfectSquare(10)).toBeFalsy()
9+
expect(perfectSquare(Infinity)).toBeFalsy()
910
})
1011
})

0 commit comments

Comments
(0)

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