freeCodeCamp Challenge Guide: Binary Agents

Had to look up parseInt - don’t understand the second argument for it at all (radix). Once that was understood, everything else seemed straight forward given the hints.

function binaryAgent(str) {
  let translate = str.split(" ")
                 .map(binary => parseInt(binary, 2))
                 .map(code => String.fromCharCode(code))
                 .join("");
  
  return translate
}

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

And here is my solution :smiley: I used some ES6 stuff

function binaryAgent(str) {
  var strToArr = str.split(' ');
  
  var binCode = [];
  
  for(var i = 0; i < strToArr.length; i++){
     binCode.push(String.fromCharCode("0b"+strToArr[i]));
  }
  
  
  return binCode.join('');
}

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

Hello, everyone. Need help!

Is there anything wrong with my code below? I can’t pass the challenge even though the code is producing the correct result.

var letters = [];

function binaryAgent(str) {
  var binary = str.split(" ");
  binary.forEach(function(el) {
    toLetter(el);
  }); 
  return letters.join("");
}

function toLetter(bits) {
  var j = 0;
  var currValue;
  var charCode = 0;
  for(var i = 7; i >= 0; i--) {
    currValue = Math.pow(2, i) * bits[j];
    j++;
    charCode += currValue;
  }
  var letter = String.fromCharCode(charCode);
  letters.push(letter);
}

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

1 Like

Try placing ‘toLetter’ function and ‘letters’ array INSIDE the binary agent function.
I think even though code is OK, tests are failing because each time test runs the letters array had the results from the last test still in it…

function binaryAgent(str) {
  var letters = [];
  function toLetter(bits) {
    var j = 0;
    var currValue;
    var charCode = 0;
    for(var i = 7; i >= 0; i--) {
      currValue = Math.pow(2, i) * bits[j];
      j++;
      charCode += currValue;
    }
    var letter = String.fromCharCode(charCode);
    letters.push(letter);
  }
  var binary = str.split(" ");
  binary.forEach(function(el) {
    toLetter(el);
  }); 
  return letters.join("");
}
2 Likes

had to learn about parseInt but was cool when I realized what could do with that.


function binaryAgent(str) {

var x = str.split(' ');
var code = [];
for (var i = 0; i < x.length; i++)
  
{
 code += String.fromCharCode(parseInt(x[i],2));
  
  
}
    
 return code.toString();
  
}

what do you reckon to this?

1 Like

function binaryAgent(str) {
function binto(num){
return String.fromCharCode(parseInt(num, 2));
}

str=str.replace(/\d{8}\s?/g,binto);

return str;
}

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

2 Likes

My solution is quite archaic, but it works and this is my level for now, so here it is:

function binaryAgent(str) {

  var arr= str.split(" ");
  var newArr=[];
  var sum;
  var result='';
  for (var i=0; i<arr.length; i++){
    sum=0;
    for (var j=0; j< arr[j].length; j++){
      if(arr[i][j] === "1"){
        switch (j){
          case 0:
            sum+= 128;
            break;
          case 1:
            sum+= 64;
            break;
          case 2:
            sum+= 32;
            break;
          case 3:
            sum+= 16;
            break;
          case 4:
            sum+= 8;
            break;
          case 5:
            sum+= 4;
            break;
          case 6:
            sum+= 2;
            break;
          case 7:
            sum+= 1;
        }
      }
    }
    newArr.push(sum);
    result+= String.fromCharCode(newArr[i]);
  }
  
  return result;
}

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

Hey everyone!

I really enjoyed this one! Here’s my solution based on parseInt() method:

    function binaryAgent(str) {
      
      var groups = str.split(" "),
          decimal = [];
      
      for (var i = 0; i < groups.length; i++) {
        decimal.push(parseInt(groups[i], 2));
      }  
      return String.fromCharCode.apply(this, decimal);
    }
    //I love FreeCodeCamp, indeed!
    binaryAgent("01001001 00100000 01101100 01101111 01110110 01100101 00100000 01000110 01110010 01100101 01100101 01000011 01101111 01100100 01100101 01000011 01100001 01101101 01110000 00100001");
2 Likes