Thursday, July 23, 2020

Binary Agents

From: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/binary-agents

Intermediate Algorithm Scripting: Binary Agents


Return an English translated sentence of the passed binary string.

The binary string will be space separated.

function es5_without_apply_binaryAgent(str) {
    return str.split(' ').map(function(letterBin) { 
        return String.fromCharCode(parseInt(letterBin, 2)); 
    })
    .join('');
}

function es5_with_apply_binaryAgent(str) {
    return String.fromCharCode.apply(
        null,
        str.split(' ').map(function(letterBin) { 
            return parseInt(letterBin, 2); 
        })
    );
}

// es6 arrow function and spread (apply's syntactic sugar) operator (...)
const binaryAgent = str => String.fromCharCode(...str.split(' ').map(bin => parseInt(bin, 2)));

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

console.log(
  binaryAgent("01001001 00100000 01101100 01101111 01110110 01100101 00100000 01000110 01110010 01100101 01100101 01000011 01101111 01100100 01100101 01000011 01100001 01101101 01110000 00100001")
);


Output:
Aren't bonfires fun!?
I love FreeCodeCamp!

No comments:

Post a Comment