Am I missing steps on Binary Agents?

Tell us what’s happening:

I’m not passing any tests and I’m returning nothing but question mark symbols or empty character boxes. I’m not exactly sure what step I’m missing because I think I have a logical setup. (Transform into array, loop through and convert to integer then to alphabet equivalent of that integer.)

My ideas:

  1. It’s HOW I’m doing it. But I don’t know how my method is inappropriate.
  2. I should be using something else instead of parseInt(). I tried charCodeAt() and it only returned binary so I tried parseInt. I also think I’m missing a step, but I think I’ve taken care of all necessary conversions…

Your guidance would be much appreciated.

Thank you.

Your code so far


function binaryAgent(str) {
 //global variable
 let final = [];
 let add = [];
 //transform string into an array
 let neu = str.split('');
//loop through new array and transform each element into an integer using parseInt with base-2 in order to convert from binary
  for(let i = 0;i < neu.length; i++){
    add.push(parseInt(neu[i], 2));
  //Convert from UTF integer to alphabet symbol
    final.push(String.fromCharCode(add[i]))
    console.log(final)
  }
  
  return final;
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/binary-agents

You are splitting on an empty string so you will have a character per array element - you may want to split on a space instead so you have a number per array element

After that we can recheck if the logic is sound (it should be)

1 Like

function binaryAgent(str) {
  console.log(str)
 //global variable
 let final = [];
 let add = [];
 //transform string into an array
 let neu = str.split(' ');
 console.log(neu)
//loop through new array and transform each element into an integer using parseInt with base-2 in order to convert from binary
  for(let i = 0;i < neu.length; i++){
    add.push(parseInt(neu, 2));
  //Convert from UTF integer to alphabet symbol
 let final = String.fromCharCode(add[i]);
  }

  
  
  return final;
}

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

I’m not passing the test. Not sure why. But if I had to guess, I think that I don’t need to use add, but my reasoning is that I wanted to have a clean, empty array that I could push the binary-converted-to-integer elements into.
Is my issue that I have too much in the code and that I need to simplify it or is everything in the correct order and the logic sound but my syntax is a little off or perhaps I don’t fully understand what the syntax is doing and so have given methods the wrong kind of perimeters or set conditions that are not appropriate.

Thanks for any kind of nudge.

You do not need the extra add array. You can simply assign the return value of (parseInt(neu, 2) to a variable (you could call it integer since that is what it is) and then you would need to convert this integer into the applicable string character (think String.fromCharCode). At this point you have a choice to make. You know that you must return a string which contains all the characters you have converted. So you can either:

  1. Push each converted character into an array (you have one called final already) and then think about a method which could help you join the elements together into a string.

OR

  1. Start off with an empty string (another variable) before the for loop and concatenate each converted character to to this string.

Finally, return the string.