From 32b77affd39fa4fc7c10d78942432e76bd90cbca Mon Sep 17 00:00:00 2001 From: Rak Laptudirm <68542775+raklaptudirm@users.noreply.github.com> Date: Thu, 6 May 2021 10:46:27 +0530 Subject: [PATCH 1/4] Create SquareRoot.js --- Maths/SquareRoot.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Maths/SquareRoot.js diff --git a/Maths/SquareRoot.js b/Maths/SquareRoot.js new file mode 100644 index 0000000000..20cdd9b4c6 --- /dev/null +++ b/Maths/SquareRoot.js @@ -0,0 +1,17 @@ +/* +* Author: Rak Laptudirm +* +* https://en.wikipedia.org/wiki/Newton%27s_method +* +* Finding the square root of a number using Newton's method. +*/ + +function sqrt (num, precision = 10) { + let sqrt = 1 + for (let i = 0; i < precision; i++) { + sqrt -= (sqrt * sqrt - num) / (2 * sqrt) + } + return sqrt +} + +export { sqrt } From a22c0486e75ef9700074348f2d138abf253f05da Mon Sep 17 00:00:00 2001 From: Rak Laptudirm <68542775+raklaptudirm@users.noreply.github.com> Date: Thu, 13 May 2021 12:46:01 +0530 Subject: [PATCH 2/4] style(Maths/SquareRoot.js): Fixed tabs according to `standard` --- Maths/SquareRoot.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Maths/SquareRoot.js b/Maths/SquareRoot.js index 20cdd9b4c6..f451183850 100644 --- a/Maths/SquareRoot.js +++ b/Maths/SquareRoot.js @@ -7,11 +7,11 @@ */ function sqrt (num, precision = 10) { - let sqrt = 1 - for (let i = 0; i < precision; i++) { - sqrt -= (sqrt * sqrt - num) / (2 * sqrt) - } - return sqrt + let sqrt = 1 + for (let i = 0; i < precision; i++) { + sqrt -= (sqrt * sqrt - num) / (2 * sqrt) + } + return sqrt } export { sqrt } From 2c1052da8bedc94cc71016232a5bbb304693b39a Mon Sep 17 00:00:00 2001 From: Rak Laptudirm <68542775+raklaptudirm@users.noreply.github.com> Date: Thu, 13 May 2021 13:07:15 +0530 Subject: [PATCH 3/4] feat(SquareRoot.js): Added TypeError --- Maths/SquareRoot.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Maths/SquareRoot.js b/Maths/SquareRoot.js index f451183850..3c8f80b2a7 100644 --- a/Maths/SquareRoot.js +++ b/Maths/SquareRoot.js @@ -7,6 +7,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 for (let i = 0; i < precision; i++) { sqrt -= (sqrt * sqrt - num) / (2 * sqrt) From 054622c9994c0123faa94cb056f0ff166c64c391 Mon Sep 17 00:00:00 2001 From: Rak Laptudirm <68542775+raklaptudirm@users.noreply.github.com> Date: Thu, 13 May 2021 13:18:51 +0530 Subject: [PATCH 4/4] style(SquareRoot.js): Fixed style --- Maths/SquareRoot.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Maths/SquareRoot.js b/Maths/SquareRoot.js index 3c8f80b2a7..be70502da7 100644 --- a/Maths/SquareRoot.js +++ b/Maths/SquareRoot.js @@ -7,10 +7,8 @@ */ 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}`) + 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 for (let i = 0; i < precision; i++) { sqrt -= (sqrt * sqrt - num) / (2 * sqrt)