Trouble with Binary agents challenge

Trouble with Binary agents challenge
0.0 0

#1

here is the code I have for Binary agents


function binaryAgent(str) {
  var charNum = [125,64,32,16,8,4,2,1];
  var splitString = str.split(""); //splits the str param
  var mult = 0; //will bes used to index charNum
  var resultofChar = []; // holds the 8 digits after they multiply
  var x = []; // holds the number returned after reduce
  var finalAns;//holds the letter returned
  
  for(var i = 0; i<splitString.length; i++){
    if(mult === 8){ 
       mult = 0;
    }
    var char = Number(splitString[i]) * charNum[mult];//mutlplies splitstring whit values in charnum 
       resultofChar.push(char); // pushes that char to the array resultofChar
       mult++; //increases mult by on so it can me mult by the next number in charNum
    if(mult === 8){ 
       x = resultofChar.reduce(function (acc ,curr){
         return acc + curr;
         },0);
     }
       finalAns= String.fromCharCode(x); // holds the letter A 
     
    //As it stands now it only applies to first 8 digits. 
  }
  return finalAns;
}

binaryAgent("01000001");

It does work for the parameter passed in. However if more string are passed in it falls apart. What can I change to make it work on multiple binary agents.?

here is the link to the challenge


#2
  • Split on spaces, that gives you every binary in a list.
  • parseInt, which takes a string and parses a number from it, takes a second parameter (a radix - so for eg for decimal it is 10, for ocatal it is 8, for binary it is…). Or you can manually calculate the value as you’re doing. Anyway, run the calculation for every element in the list.
  • join the list back up.

#3

Holy shit. I think i got it now. thanks


#4

if you are interested in making your code compact, here is one line solution.

function binaryAgent(str) {
  return str.split(/\s/g).map((x) => x = String.fromCharCode(parseInt(x, 2))).join("");
}
//returns "Aren't bonfires fun!?"

binaryAgent("01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111");

#5

Well, I didn’t know how to find the binary code on JavaScript so I created an Object with all of them and after that, I just called them.

return (str.split(" ")).map(x=> binary[x]).join("")

function binaryAgent(str) {
  
  

let binary = {
'01000001'	:	'A'	,
'01000010'	:	'B'	,
'01000011'	:	'C'	,
'01000100'	:	'D'	,
'01000101'	:	'E'	,
'01000110'	:	'F'	,
'01000111'	:	'G'	,
'01001000'	:	'H'	,
'01001001'	:	'I'	,
'01001010'	:	'J'	,
'01001011'	:	'K'	,
'01001100'	:	'L'	,
'01001101'	:	'M'	,
'01001110'	:	'N'	,
'01001111'	:	'O'	,
'01010000'	:	'P'	,
'01010001'	:	'Q'	,
'01010010'	:	'R'	,
'01010011'	:	'S'	,
'01010100'	:	'T'	,
'01010101'	:	'U'	,
'01010110'	:	'V'	,
'01010111'	:	'W'	,
'01011000'	:	'X'	,
'01011001'	:	'Y'	,
'01011010'	:	'Z'	,
'01011011'	:	'['	,
'01011101'	:	']'	,
'01011110'	:	'^'	,
'01011111'	:	'_'	,
'01100000'	:	'`'	,
'01100001'	:	'a'	,
'01100010'	:	'b'	,
'01100011'	:	'c'	,
'01100100'	:	'd'	,
'01100101'	:	'e'	,
'01100110'	:	'f'	,
'01100111'	:	'g'	,
'01101000'	:	'h'	,
'01101001'	:	'i'	,
'01101010'	:	'j'	,
'01101011'	:	'k'	,
'01101100'	:	'l'	,
'01101101'	:	'm'	,
'01101110'	:	'n'	,
'01101111'	:	'o'	,
'01110000'	:	'p'	,
'01110001'	:	'q'	,
'01110010'	:	'r'	,
'01110011'	:	's'	,
'01110100'	:	't'	,
'01110101'	:	'u'	,
'01110110'	:	'v'	,
'01110111'	:	'w'	,
'01111000'	:	'x'	,
'01111001'	:	'y'	,
'01111010'	:	'z'	,
'01111011'	:	'{'	,
'01111100'	:	'|'	,
'01111101'	:	'}'	,
'01111110'	:	'~'	,
'01111111'	:	'DEL'	,
'00100111'  : "'" ,
'00100000'  : " ",
'00100001'  : '!',
'00111111'  : '?'
}




  
return (str.split(" ")).map(x=> binary[x]).join("")
 
  
 
;
}


#6

That will work fine, and it’s a good solution, but you need to bear in mind there are around 100,000 mapped characters in Unicode. For this challenge, what you’ve written will pass all the tests no problem; if you can guarantee a character set matching what you have, there isn’t an issue.

But what @farhankk360 suggested does what you’re currently doing, plus it will deal with any characters that you have missed out in your solution (for example, what if there was an &, or "? You would be forever adding more characters to your lookup table):

  • Each unicode character has a character code.
  • You can convert a character code to a unicode character using the function String.fromCharCode.
  • String.fromCharCode takes an integer (it will match a number in the decimal column here).
  • You can convert a string representing a binary to an integer using parseInt(myBinaryString, 2).
  • So you could just hand this function to map:
    function binaryToChar(binString)
      let code = parseInt(binString, 2);
      return String.fromCharCode(code);
    }