File tree 2 files changed +44
-0
lines changed
2 files changed +44
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ A program to demonstrate the implementation of the signum function,
3
+ also known as the sign function, in JavaScript.
4
+
5
+ The signum function is an odd mathematical function, which returns the
6
+ sign of the provided real number.
7
+ It can return 3 values: 1 for values greater than zero, 0 for zero itself,
8
+ and -1 for values less than zero
9
+
10
+ Wikipedia: https://en.wikipedia.org/wiki/Sign_function
11
+ */
12
+
13
+ /**
14
+ * @param {Number } input
15
+ * @returns {-1 | 0 | 1 | NaN } sign of input (and NaN if the input is not a number)
16
+ */
17
+ function signum ( input ) {
18
+ if ( input === 0 ) return 0
19
+ if ( input > 0 ) return 1
20
+ if ( input < 0 ) return - 1
21
+
22
+ return NaN
23
+ }
24
+
25
+ export { signum }
Original file line number Diff line number Diff line change
1
+ import { signum } from '../Signum'
2
+
3
+ describe ( 'The sign of a number' , ( ) => {
4
+ it ( 'Sign of 10' , ( ) => {
5
+ expect ( signum ( 10 ) ) . toBe ( 1 )
6
+ } )
7
+
8
+ it ( 'Sign of 0' , ( ) => {
9
+ expect ( signum ( 0 ) ) . toBe ( 0 )
10
+ } )
11
+
12
+ it ( 'Sign of -420' , ( ) => {
13
+ expect ( signum ( - 420 ) ) . toBe ( - 1 )
14
+ } )
15
+
16
+ it ( 'Sign of NaN' , ( ) => {
17
+ expect ( signum ( NaN ) ) . toBe ( NaN )
18
+ } )
19
+ } )
You can’t perform that action at this time.
0 commit comments