Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 486c5e8

Browse files
🕷️ 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)

AltStyle によって変換されたページ (->オリジナル) /