File tree 7 files changed +159
-0
lines changed
7 files changed +159
-0
lines changed Original file line number Diff line number Diff line change
1
+ ## Number System
2
+
3
+ Every software engineer should know these conversions.
4
+
5
+ #TODO:
6
+
7
+ Yet to handle decimal digits.
Original file line number Diff line number Diff line change
1
+ //using reduce
2
+ const solution = binary => {
3
+ return binary . split ( "" ) . reduce ( ( result , bit , index ) => {
4
+ return (
5
+ result + parseInt ( binary [ binary . length - index - 1 ] ) * Math . pow ( 2 , index )
6
+ ) ;
7
+ } , 0 ) ;
8
+ } ;
9
+
10
+ const usingForLoops = binary => {
11
+ let result = 0 ,
12
+ power = 1 ;
13
+
14
+ for ( let index = binary . length - 1 ; index >= 0 ; index -- ) {
15
+ if ( binary [ index ] === "1" ) {
16
+ result += power * binary [ index ] ;
17
+ }
18
+ power *= 2 ;
19
+ }
20
+ return result ;
21
+ } ;
22
+
23
+ console . log ( usingForLoops ( "101" ) ) ;
Original file line number Diff line number Diff line change
1
+ const getHex = group => {
2
+ const mapping = { } ;
3
+ mapping [ "0000" ] = "0" ;
4
+ mapping [ "0001" ] = "1" ;
5
+ mapping [ "0010" ] = "2" ;
6
+ mapping [ "0011" ] = "3" ;
7
+ mapping [ "0100" ] = "4" ;
8
+ mapping [ "0101" ] = "5" ;
9
+ mapping [ "0110" ] = "6" ;
10
+ mapping [ "0111" ] = "7" ;
11
+ mapping [ "1000" ] = "8" ;
12
+ mapping [ "1001" ] = "9" ;
13
+ mapping [ "1010" ] = "A" ;
14
+ mapping [ "1011" ] = "B" ;
15
+ mapping [ "1100" ] = "C" ;
16
+ mapping [ "1101" ] = "D" ;
17
+ mapping [ "1110" ] = "E" ;
18
+ mapping [ "1111" ] = "F" ;
19
+ return mapping [ group ] ;
20
+ } ;
21
+ const solution = binary => {
22
+ let result = [ ] ;
23
+ const length = binary . length ;
24
+ const extra = length % 4 ;
25
+ for ( let i = length ; i > extra ; i -= 4 ) {
26
+ result . unshift ( getHex ( binary . slice ( i - 4 , i ) ) ) ;
27
+ }
28
+
29
+ result . unshift (
30
+ getHex ( new Array ( 4 - extra ) . fill ( "0" ) . join ( "" ) + binary . slice ( 0 , extra ) )
31
+ ) ;
32
+
33
+ return result . join ( "" ) ;
34
+ } ;
Original file line number Diff line number Diff line change
1
+ const solution = decimal => {
2
+ if ( decimal < 2 ) {
3
+ return decimal + "" ;
4
+ }
5
+ return solution ( Math . floor ( decimal / 2 ) ) + ( decimal % 2 ) ;
6
+ } ;
7
+
8
+ console . log ( solution ( 1 ) ) ;
Original file line number Diff line number Diff line change
1
+ //can have a mapping instead of this
2
+ const getHex = digit => {
3
+ digit = parseInt ( digit ) ;
4
+ if ( digit > 9 ) {
5
+ return String . fromCharCode ( 55 + digit ) ;
6
+ }
7
+ return digit + "" ;
8
+ } ;
9
+
10
+ const solution = decimal => {
11
+ if ( decimal < 16 ) {
12
+ return getHex ( decimal ) ;
13
+ }
14
+ return solution ( Math . floor ( decimal / 16 ) ) + getHex ( decimal % 2 ) ;
15
+ } ;
16
+
17
+ console . log ( solution ( 160 ) ) ;
Original file line number Diff line number Diff line change
1
+ const getBinary = group => {
2
+ const mapping = { } ;
3
+ mapping [ "0" ] = "0000" ;
4
+ mapping [ "1" ] = "0001" ;
5
+ mapping [ "2" ] = "0010" ;
6
+ mapping [ "3" ] = "0011" ;
7
+ mapping [ "4" ] = "0100" ;
8
+ mapping [ "5" ] = "0101" ;
9
+ mapping [ "6" ] = "0110" ;
10
+ mapping [ "7" ] = "0111" ;
11
+ mapping [ "8" ] = "1000" ;
12
+ mapping [ "9" ] = "1001" ;
13
+ mapping [ "A" ] = "1010" ;
14
+ mapping [ "B" ] = "1011" ;
15
+ mapping [ "C" ] = "1100" ;
16
+ mapping [ "D" ] = "1101" ;
17
+ mapping [ "E" ] = "1110" ;
18
+ mapping [ "F" ] = "1111" ;
19
+ return mapping [ group ] ;
20
+ } ;
21
+
22
+ const solution = hex => {
23
+ return hex
24
+ . split ( "" )
25
+ . map ( digit => {
26
+ return getBinary ( digit ) ;
27
+ } )
28
+ . join ( "" ) ;
29
+ } ;
Original file line number Diff line number Diff line change
1
+ let convertHex = digit => {
2
+ return mapping [ digit ] ;
3
+ } ;
4
+ //using reduce
5
+ const solution = hex => {
6
+ return hex . split ( "" ) . reduce ( ( result , bit , index ) => {
7
+ return (
8
+ result + convertHex ( hex [ hex . length - index - 1 ] ) * Math . pow ( 16 , index )
9
+ ) ;
10
+ } , 0 ) ;
11
+ } ;
12
+
13
+ const usingForLoops = hex => {
14
+ let result = 0 ,
15
+ power = 1 ;
16
+
17
+ for ( let index = hex . length - 1 ; index >= 0 ; index -- ) {
18
+ result += power * convertHex ( hex [ index ] ) ;
19
+ power *= 16 ;
20
+ }
21
+ return result ;
22
+ } ;
23
+ const mapping = { } ;
24
+ mapping [ "0" ] = 0 ;
25
+ mapping [ "1" ] = 1 ;
26
+ mapping [ "2" ] = 2 ;
27
+ mapping [ "3" ] = 3 ;
28
+ mapping [ "4" ] = 4 ;
29
+ mapping [ "5" ] = 5 ;
30
+ mapping [ "6" ] = 6 ;
31
+ mapping [ "7" ] = 7 ;
32
+ mapping [ "8" ] = 8 ;
33
+ mapping [ "9" ] = 9 ;
34
+ mapping [ "A" ] = 10 ;
35
+ mapping [ "B" ] = 11 ;
36
+ mapping [ "C" ] = 12 ;
37
+ mapping [ "D" ] = 13 ;
38
+ mapping [ "E" ] = 14 ;
39
+ mapping [ "F" ] = 15 ;
40
+
41
+ console . log ( solution ( "A0" ) ) ;
You can’t perform that action at this time.
0 commit comments