Skip to content

Commit e17d295

Browse files
committed
Update app.html
bug fix decimalToBinaryV1 when input string = 0 result was before empty string '' bug fix binaryToHex function to correctly pad original string and chunk it in 4bit groups
1 parent ca5c0bb commit e17d295

File tree

1 file changed

+38
-11
lines changed

1 file changed

+38
-11
lines changed

app.html

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ <h1>
8181

8282
/* array.reduce();
8383
group 4 bits then translate */
84-
const binaryToHex= (binaryString)=> {
84+
const binaryToHex= (binaryString0)=> {
8585
const hexNumbers= new Map();
8686

8787
hexNumbers.set('0000', '0');
@@ -107,15 +107,33 @@ <h1>
107107
// group binary string into 4 bit chunks
108108
const array= [];
109109

110-
for(let i=0; i< binaryString.length; i+= 4){
111-
let chunk= binaryString.substring(0, 4);
110+
console.log('*** binaryToHex ***', 'binaryString0= ', binaryString0);
111+
// important; translate binary string from right to left
112112

113-
//pad left with 0s is bit string <4
114-
if(chunk.length < 4){
115-
chunk= chunk.padEnd(4, '0');
116-
}
113+
let length= binaryString0.length;
114+
115+
/*
116+
- if the string is not a multiple of 4;
117+
left-pad it.
118+
119+
- if binaryString0='' then pad with 4
120+
*/
121+
const missing= length === 0 ? 4 : ((length % 4 === 0) ? 0 : (4 - length % 4));
122+
const binaryString= binaryString0.padStart(length + missing, '0');
123+
124+
const newLength= binaryString.length;
125+
126+
127+
for(let i=0; i< newLength; i+= 4){
128+
let chunk= binaryString.substring(i, i+4);
129+
130+
console.log('-- chunk=', chunk);
117131

118-
array.push(hexNumbers.get(chunk));
132+
const value= hexNumbers.get(chunk);
133+
134+
array.push(value);
135+
136+
console.log('--value=', value);
119137
}
120138

121139
const hexString= array.join('');
@@ -131,13 +149,22 @@ <h1>
131149
using array.unshift(r)
132150
*/
133151
const decimalToBinaryV1= (decimalString)=> {
134-
/*division remainers*/
152+
/*
153+
- division remainers
154+
*/
135155
const array= [];
136156

137157
/*quotient mutated*/
138158
let i= BigInt(decimalString);
139-
140159

160+
/*
161+
- if decimalString='0' then
162+
while loop will NOT run;
163+
*/
164+
if(i === BigInt(0)){
165+
array.unshift(BigInt(0));
166+
}
167+
141168
/*stop when quotient = 0*/
142169
while ( i != BigInt(0) ){
143170

@@ -495,7 +522,7 @@ <h2>Invalid Input</h2>
495522
super(props);
496523
}
497524
componentDidMount(){
498-
this.props['do-initial-onInputDecimal']('1234');
525+
this.props['do-initial-onInputDecimal']('256');
499526
}
500527
render(){
501528
const {

0 commit comments

Comments
 (0)