1
- const d2f = num => {
1
+ const d2f = ( num , neg ) => {
2
2
const helper = ( f1 , f2 , n ) => {
3
3
if ( f2 > n ) {
4
4
return [ n , "1" ] ;
@@ -10,46 +10,88 @@ const d2f = num => {
10
10
return [ rem , "0" + b ] ;
11
11
}
12
12
}
13
- if ( num <= 0 ) {
14
- return "Number must be greater than 0" ;
13
+
14
+ const negHelper = ( f1 , f2 , sum , n ) => {
15
+ if ( Math . abs ( sum ) > Math . abs ( n ) ) {
16
+ return [ n , "1" ] ;
17
+ }
18
+ if ( ( n < 0 && f1 < 0 ) || ( n > 0 && f1 > 0 ) ) {
19
+ sum += f1 ;
20
+ }
21
+ [ rem , b ] = negHelper ( f2 , f1 + - f2 , sum , n ) ;
22
+ const canBe1 = b . charAt ( 0 ) !== '1' || b . length === 1 ;
23
+ if ( Math . abs ( rem - f1 ) < Math . abs ( rem ) && canBe1 ) {
24
+ return [ rem - f1 , "1" + b ] ;
25
+ } else {
26
+ return [ rem , "0" + b ] ;
27
+ }
28
+ }
29
+
30
+ if ( neg ) {
31
+ if ( num === 0 ) {
32
+ return "Number cannot be 0" ;
33
+ }
34
+ return negHelper ( 1 , - 1 , 0 , num ) [ 1 ] ;
35
+ } else {
36
+ if ( num <= 0 ) {
37
+ return "Number must be greater than 0" ;
38
+ }
39
+ return helper ( 1 , 1 , num ) [ 1 ] ;
15
40
}
16
- return helper ( 1 , 1 , num ) [ 1 ] ;
17
41
} ;
18
42
19
- const f2d = b => {
43
+ const f2d = ( enc , neg ) => {
44
+ const n = neg ? - 1 : 1 ;
20
45
const helper = ( f1 , f2 , i ) => {
21
- if ( i >= b . length - 1 ) {
46
+ if ( i >= enc . length - 1 ) {
22
47
return 0 ;
23
48
}
24
- const char = b . charAt ( i ) ;
49
+ const char = enc . charAt ( i ) ;
25
50
if ( char == '1' ) {
26
- return f2 + helper ( f2 , f1 + f2 , i + 1 ) ;
51
+ return ( neg ? f1 : f2 ) + helper ( f2 , f1 + n * f2 , i + 1 ) ;
27
52
} else if ( char == '0' ) {
28
- return helper ( f2 , f1 + f2 , i + 1 ) ;
53
+ return helper ( f2 , f1 + n * f2 , i + 1 ) ;
29
54
}
30
55
}
31
- for ( var i = 0 ; i < b . length ; i ++ ) {
32
- switch ( b . charAt ( i ) ) {
56
+ for ( var i = 0 ; i < enc . length ; i ++ ) {
57
+ switch ( enc . charAt ( i ) ) {
33
58
case '1' :
34
59
case '0' :
35
60
break ;
36
61
default :
37
- return `Invalid character at position ${ i } : ${ b . charAt ( i ) } ` ;
62
+ return `Invalid character at position ${ i } : ${ enc . charAt ( i ) } ` ;
38
63
}
39
64
}
40
- if ( b . length < 2 || b . charAt ( b . length - 1 ) != '1' || b . charAt ( b . length - 2 ) != '1' ) {
65
+ if ( enc . length < 2 || enc . charAt ( enc . length - 1 ) != '1' || enc . charAt ( enc . length - 2 ) != '1' ) {
41
66
return "Last two bits must be 1" ;
42
67
}
43
- return helper ( 1 , 1 , 0 ) ;
68
+ return helper ( 1 , n * 1 , 0 ) ;
44
69
} ;
45
70
46
- document . getElementById ( "d2f" ) . addEventListener ( "click" , ( ) => {
47
- const input = document . getElementById ( "input" ) . value ;
48
- const output = d2f ( parseInt ( input ) ) ;
49
- document . getElementById ( "output" ) . value = output ;
50
- } ) ;
51
- document . getElementById ( "f2d" ) . addEventListener ( "click" , ( ) => {
52
- const input = document . getElementById ( "input" ) . value ;
53
- const output = f2d ( input ) ;
71
+ document . getElementById ( "convert" ) . addEventListener ( "click" , ( ) => {
72
+ const inputEncoding = document . querySelector ( 'input[name="from"]:checked' ) . value ;
73
+ const outputEncoding = document . querySelector ( 'input[name="to"]:checked' ) . value ;
74
+
75
+ const input = document . getElementById ( "input" ) . value . trim ( ) ;
76
+ var output ;
77
+ if ( inputEncoding === outputEncoding ) {
78
+ output = input ;
79
+ } else if ( inputEncoding === "fibonacci" ) {
80
+ const decimal = f2d ( input , false ) ;
81
+ if ( outputEncoding === "decimal" ) {
82
+ output = decimal ;
83
+ } else {
84
+ output = ( / ^ \d + $ / . test ( decimal ) ) ? d2f ( decimal , true ) : decimal ;
85
+ }
86
+ } else if ( inputEncoding === "decimal" ) {
87
+ output = d2f ( input , outputEncoding === "negafibonacci" ) ;
88
+ } else if ( inputEncoding === "negafibonacci" ) {
89
+ const decimal = f2d ( input , true ) ;
90
+ if ( outputEncoding === "decimal" ) {
91
+ output = decimal
92
+ } else {
93
+ output = ( / ^ \d + $ / . test ( decimal ) ) ? d2f ( decimal , false ) : decimal ;
94
+ }
95
+ }
54
96
document . getElementById ( "output" ) . value = output . toString ( ) ;
55
97
} ) ;
0 commit comments