File tree 2 files changed +49
-0
lines changed
2 files changed +49
-0
lines changed Original file line number Diff line number Diff line change
1
+ // --- Directions
2
+ // Given an integer, return an integer that is the reverse
3
+ // ordering of numbers.
4
+ // --- Examples
5
+ // reverseInt(15) === 51
6
+ // reverseInt(981) === 189
7
+ // reverseInt(500) === 5
8
+ // reverseInt(-15) === -51
9
+ // reverseInt(-90) === -9
10
+
11
+ // Tips to solve the problem
12
+ // - Convert number to string by using toString function
13
+ // - Check if it is a sign number or not by using Math.sign() function.
14
+ // - Convert string to number by using parseInt method
15
+
16
+ function reverseInt ( n ) {
17
+ const reversed = n
18
+ . toString ( )
19
+ . split ( '' )
20
+ . reverse ( )
21
+ . join ( '' ) ;
22
+ return parseInt ( reversed ) * Math . sign ( n ) ;
23
+ }
24
+
25
+
26
+ module . exports = reverseInt ;
Original file line number Diff line number Diff line change
1
+ const reverseInt = require ( './index' ) ;
2
+
3
+ test ( 'ReverseInt function exists' , ( ) => {
4
+ expect ( reverseInt ) . toBeDefined ( ) ;
5
+ } ) ;
6
+
7
+ test ( 'ReverseInt handles 0 as an input' , ( ) => {
8
+ expect ( reverseInt ( 0 ) ) . toEqual ( 0 ) ;
9
+ } ) ;
10
+
11
+ test ( 'ReverseInt flips a positive number' , ( ) => {
12
+ expect ( reverseInt ( 5 ) ) . toEqual ( 5 ) ;
13
+ expect ( reverseInt ( 15 ) ) . toEqual ( 51 ) ;
14
+ expect ( reverseInt ( 90 ) ) . toEqual ( 9 ) ;
15
+ expect ( reverseInt ( 2359 ) ) . toEqual ( 9532 ) ;
16
+ } ) ;
17
+
18
+ test ( 'ReverseInt flips a negative number' , ( ) => {
19
+ expect ( reverseInt ( - 5 ) ) . toEqual ( - 5 ) ;
20
+ expect ( reverseInt ( - 15 ) ) . toEqual ( - 51 ) ;
21
+ expect ( reverseInt ( - 90 ) ) . toEqual ( - 9 ) ;
22
+ expect ( reverseInt ( - 2359 ) ) . toEqual ( - 9532 ) ;
23
+ } ) ;
You can’t perform that action at this time.
0 commit comments