Skip to content

Instantly share code, notes, and snippets.

@nirajkrz
Forked from anonymous/bonfire-binary-agents.js
Created December 3, 2015 02:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nirajkrz/14e60f38a94dc1a4381c to your computer and use it in GitHub Desktop.
Save nirajkrz/14e60f38a94dc1a4381c to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/nirajkrz 's solution for Bonfire: Binary Agents
// Bonfire: Binary Agents
// Author: @nirajkrz
// Challenge: http://www.freecodecamp.com/challenges/bonfire-binary-agents
// Learn to Code at Free Code Camp (www.freecodecamp.com)
//Return an English translated sentence of the passed binary string.
function binaryAgent(str) {
// Separate the binary code by space.
str = str.split(' ');
var power;
var decValue = 0;
var sentence = '';
// Check each binary number from the array.
for (var s = 0; s < str.length; s++) {
// Check each bit from binary number
for (var t = 0; t < str[s].length; t++) {
// This only takes into consideration the active ones.
if (str[s][t] == 1) {
// This is quivalent to 2 ** position
power = Math.pow(2, +str[s].length - t - 1);
decValue += power;
// Record the decimal value by adding the number to the previous one.
}
}
// After the binary number is converted to decimal, convert it to string and store
sentence += (String.fromCharCode(decValue));
// Reset decimal value for next binary number.
decValue = 0;
}
return sentence;
}
binaryAgent("01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment