Skip to content

Commit 486c5e8

Browse files
committed
🕷️ Fix NaN issue in signum
1 parent 40e9177 commit 486c5e8

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

Maths/Signum.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@
1212

1313
/**
1414
* @param {Number} input
15-
* @returns {-1 | 0 | 1} sign of input
15+
* @returns {-1 | 0 | 1 | NaN} sign of input (and NaN if the input is NaN)
1616
*/
1717
function signum (input) {
18-
if (input === 0) return 0
18+
if(isNaN(input)) return NaN
19+
else if (input === 0) return 0
1920
else if (input < 0) return -1
2021
else return 1
2122
}

Maths/test/Signum.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,8 @@ describe('The sign of a number', () => {
1212
it('Sign of -420', () => {
1313
expect(signum(-420)).toBe(-1)
1414
})
15+
16+
it('Sign of NaN', () => {
17+
expect(signum(NaN)).toBe(NaN)
18+
})
1519
})

0 commit comments

Comments
 (0)
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