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:
Ricardo Fernández Serrata
2022-08-12 13:44:25 -04:00
committed by GitHub
parent e9b8b136b9
commit 979046897d
4 changed files with 9 additions and 4 deletions

View File

@@ -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 }