From cbf8a66fe88c27d0683140151fbcff6c08ee3e10 Mon Sep 17 00:00:00 2001 From: SpiderMath <71999854+SpiderMath@users.noreply.github.com> Date: Sun, 27 Nov 2022 00:34:07 +0530 Subject: [PATCH 1/6] =?UTF-8?q?=F0=9F=A7=A9=20Added=20signum=20implementai?= =?UTF-8?q?on?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Maths/Signum.js | 23 +++++++++++++++++++++++ Maths/test/Signum.test.js | 15 +++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 Maths/Signum.js create mode 100644 Maths/test/Signum.test.js diff --git a/Maths/Signum.js b/Maths/Signum.js new file mode 100644 index 0000000000..8b263333e2 --- /dev/null +++ b/Maths/Signum.js @@ -0,0 +1,23 @@ +/* + A program to demonstrate the implementation of the signum function, + also known as the sign function, in JavaScript. + + The signum function is an odd mathematical function, which returns the + sign of the provided real number. + It can return 3 values: 1 for values greater than zero, 0 for zero itself, + and -1 for values less than zero + + Wikipedia: https://en.wikipedia.org/wiki/Sign_function +*/ + +/** + * @param {Number} input + * @returns {-1 | 0 | 1} sign of input + */ +function signum (input) { + if (input === 0) return 0 + else if (input < 0) return -1 + else return 1 +} + +export { signum } diff --git a/Maths/test/Signum.test.js b/Maths/test/Signum.test.js new file mode 100644 index 0000000000..e9e7d3a048 --- /dev/null +++ b/Maths/test/Signum.test.js @@ -0,0 +1,15 @@ +import { signum } from '../Signum' + +describe('The sign of a number', () => { + it('Sign of 10', () => { + expect(signum(10)).toBe(1) + }) + + it('Sign of 0', () => { + expect(signum(10)).toBe(0) + }) + + it('Sign of -420', () => { + expect(signum(-420)).toBe(-1) + }) +}) From 40e917759fe200e42a2210cd7e5382b35321ad2d Mon Sep 17 00:00:00 2001 From: SpiderMath <71999854+SpiderMath@users.noreply.github.com> Date: Sun, 27 Nov 2022 00:34:18 +0530 Subject: [PATCH 2/6] Fix typo in tests --- Maths/test/Signum.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Maths/test/Signum.test.js b/Maths/test/Signum.test.js index e9e7d3a048..c050d25f7b 100644 --- a/Maths/test/Signum.test.js +++ b/Maths/test/Signum.test.js @@ -6,7 +6,7 @@ describe('The sign of a number', () => { }) it('Sign of 0', () => { - expect(signum(10)).toBe(0) + expect(signum(0)).toBe(0) }) it('Sign of -420', () => { From 486c5e88ff3b863e42400f3c599757cd64bcc3c9 Mon Sep 17 00:00:00 2001 From: SpiderMath <71999854+SpiderMath@users.noreply.github.com> Date: Mon, 28 Nov 2022 09:12:47 +0530 Subject: [PATCH 3/6] =?UTF-8?q?=F0=9F=95=B7=EF=B8=8F=20Fix=20NaN=20issue?= =?UTF-8?q?=20in=20signum?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Maths/Signum.js | 5 +++-- Maths/test/Signum.test.js | 4 ++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/Maths/Signum.js b/Maths/Signum.js index 8b263333e2..fd3e2e95fd 100644 --- a/Maths/Signum.js +++ b/Maths/Signum.js @@ -12,10 +12,11 @@ /** * @param {Number} input - * @returns {-1 | 0 | 1} sign of input + * @returns {-1 | 0 | 1 | NaN} sign of input (and NaN if the input is NaN) */ function signum (input) { - if (input === 0) return 0 + if(isNaN(input)) return NaN + else if (input === 0) return 0 else if (input < 0) return -1 else return 1 } diff --git a/Maths/test/Signum.test.js b/Maths/test/Signum.test.js index c050d25f7b..ac00676a00 100644 --- a/Maths/test/Signum.test.js +++ b/Maths/test/Signum.test.js @@ -12,4 +12,8 @@ describe('The sign of a number', () => { it('Sign of -420', () => { expect(signum(-420)).toBe(-1) }) + + it('Sign of NaN', () => { + expect(signum(NaN)).toBe(NaN) + }) }) From 817a7e013013bb6d2c05e94914678f11063a8451 Mon Sep 17 00:00:00 2001 From: SpiderMath <71999854+SpiderMath@users.noreply.github.com> Date: Mon, 28 Nov 2022 09:13:23 +0530 Subject: [PATCH 4/6] Formatting issues fix --- Maths/Signum.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Maths/Signum.js b/Maths/Signum.js index fd3e2e95fd..55d44bfd0d 100644 --- a/Maths/Signum.js +++ b/Maths/Signum.js @@ -15,7 +15,7 @@ * @returns {-1 | 0 | 1 | NaN} sign of input (and NaN if the input is NaN) */ function signum (input) { - if(isNaN(input)) return NaN + if (isNaN(input)) return NaN else if (input === 0) return 0 else if (input < 0) return -1 else return 1 From da1866df6f06b1654d67ae0fd4d9a035e17e8128 Mon Sep 17 00:00:00 2001 From: SpiderMath <71999854+SpiderMath@users.noreply.github.com> Date: Mon, 5 Dec 2022 22:49:04 +0530 Subject: [PATCH 5/6] Fixed up the signum function --- Maths/Signum.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Maths/Signum.js b/Maths/Signum.js index 55d44bfd0d..ddee192f6a 100644 --- a/Maths/Signum.js +++ b/Maths/Signum.js @@ -12,13 +12,14 @@ /** * @param {Number} input - * @returns {-1 | 0 | 1 | NaN} sign of input (and NaN if the input is NaN) + * @returns {-1 | 0 | 1 | NaN} sign of input (and NaN if the input is not a number) */ function signum (input) { - if (isNaN(input)) return NaN - else if (input === 0) return 0 + if (input === 0) return 0 + else if (input > 0) return 1 else if (input < 0) return -1 - else return 1 + + return NaN } export { signum } From 4cc3d5107f7916b61d2343938acbb7651adc3512 Mon Sep 17 00:00:00 2001 From: SpiderMath <71999854+SpiderMath@users.noreply.github.com> Date: Wed, 7 Dec 2022 00:26:20 +0530 Subject: [PATCH 6/6] Removed else from else-ifs --- Maths/Signum.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Maths/Signum.js b/Maths/Signum.js index ddee192f6a..35047cac18 100644 --- a/Maths/Signum.js +++ b/Maths/Signum.js @@ -16,8 +16,8 @@ */ function signum (input) { if (input === 0) return 0 - else if (input > 0) return 1 - else if (input < 0) return -1 + if (input > 0) return 1 + if (input < 0) return -1 return NaN } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy