How to output   from binary words?

Tell us what’s happening:

I’ve been working on a JavaScript algorithm challenge, and I stumbled onto a feature I haven’t learned yet. My console output transforms to a when I post, so it’s in the title.

What is this?

Is this a specific native library of JavaScript? I know the output is caused by using the fromCharCode() method, but none of these characters are in the ASCII chart.

Your code so far


function binaryAgent(str) {

var word = "";
var sentence = "";
var sum = 0;
var strArr = str.split(" ");

while (strArr.length > 0) {
  word = strArr.splice(0, 1).join("").split("");
  //console.log(word);
  sum = 0;

  for (let i = 0; i < word.length; i++) {
    sum += (word.pop() * 2 ** i);
  }

  sentence += String.fromCharCode(sum);

}

console.log(sentence);
return str;
}

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 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36.

Challenge: Binary Agents

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

I just want to clarify that I don’t need help with the challenge. It appears the special characters are related to the beginning of the ASCII chart, but I don’t know how or why.

My code in case anyone is interested:
function binaryAgent(str) {

  var word = "";
  var sentence = "";
  var sum = 0;
  var strArr = str.split(" ");

  while (strArr.length > 0) {
    word = strArr.splice(0, 1).join("").split("");
    sum = 0;

    for (let i = 0; i < 8; i++) {
      sum += (word.pop() * (2 ** i));
    }
    sentence += String.fromCharCode(sum);
  }
  return sentence;
}

Hi Jonathan,
I understand your stumbled, in javascript everything unless primitive types are objects. “The primitive types boolean, string and number can be wrapped by their object counterparts”.
String is one objet that can be instantiated and have properties, methods … One of then is .fromCharCode(). The category Javascript object oriented deepen this theme.
Stay the link: https://javascriptweblog.wordpress.com/2010/09/27/the-secret-life-of-javascript-primitives/

And relative to ASCII table try Unicode table. - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCharCode

1 Like