mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-04 15:39:42 +08:00
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
This commit is contained in:

committed by
GitHub

parent
e9b8b136b9
commit
979046897d
@ -2,8 +2,9 @@
|
|||||||
* Author: dephraiim
|
* Author: dephraiim
|
||||||
* License: GPL-3.0 or later
|
* License: GPL-3.0 or later
|
||||||
*
|
*
|
||||||
|
* This uses `round` instead of `floor` or `trunc`, to guard against potential `cbrt` accuracy errors
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const perfectCube = (num) => Math.round(num ** (1 / 3)) ** 3 === num
|
const perfectCube = (num) => Number.isFinite(num) && Math.round(Math.cbrt(num)) ** 3 === num
|
||||||
|
|
||||||
export { perfectCube }
|
export { perfectCube }
|
||||||
|
@ -2,8 +2,9 @@
|
|||||||
* Author: dephraiim
|
* Author: dephraiim
|
||||||
* License: GPL-3.0 or later
|
* 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 }
|
export { perfectSquare }
|
||||||
|
@ -3,8 +3,10 @@ import { perfectCube } from '../PerfectCube'
|
|||||||
describe('PerfectCube', () => {
|
describe('PerfectCube', () => {
|
||||||
it('should return true for a perfect cube', () => {
|
it('should return true for a perfect cube', () => {
|
||||||
expect(perfectCube(125)).toBeTruthy()
|
expect(perfectCube(125)).toBeTruthy()
|
||||||
|
expect(perfectCube(-125)).toBeTruthy()
|
||||||
})
|
})
|
||||||
it('should return false for a non perfect cube', () => {
|
it('should return false for a non perfect cube', () => {
|
||||||
expect(perfectCube(100)).toBeFalsy()
|
expect(perfectCube(100)).toBeFalsy()
|
||||||
|
expect(perfectCube(Infinity)).toBeFalsy()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -1,10 +1,11 @@
|
|||||||
import { perfectSquare } from '../PerfectSquare'
|
import { perfectSquare } from '../PerfectSquare'
|
||||||
|
|
||||||
describe('PerfectSquare', () => {
|
describe('PerfectSquare', () => {
|
||||||
it('should return true for a perfect cube', () => {
|
it('should return true for a perfect square', () => {
|
||||||
expect(perfectSquare(16)).toBeTruthy()
|
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(10)).toBeFalsy()
|
||||||
|
expect(perfectSquare(Infinity)).toBeFalsy()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
Reference in New Issue
Block a user