mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 00:01:37 +08:00
feat(SquareRoot.js): Added TypeError
This commit is contained in:
@ -7,6 +7,10 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
function sqrt (num, precision = 10) {
|
function sqrt (num, precision = 10) {
|
||||||
|
if (!Number.isFinite(num))
|
||||||
|
throw new TypeError(`Expected a number, received ${typeof num}`)
|
||||||
|
if (!Number.isFinite(precision))
|
||||||
|
throw new TypeError(`Expected a number, received ${typeof precision}`)
|
||||||
let sqrt = 1
|
let sqrt = 1
|
||||||
for (let i = 0; i < precision; i++) {
|
for (let i = 0; i < precision; i++) {
|
||||||
sqrt -= (sqrt * sqrt - num) / (2 * sqrt)
|
sqrt -= (sqrt * sqrt - num) / (2 * sqrt)
|
||||||
|
Reference in New Issue
Block a user