Created
December 3, 2015 02:56
-
-
Save anonymous/e8f367a1bc57579a3afc to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/nirajkrz 's solution for Bonfire: Binary Agents
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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