Skip to content

Instantly share code, notes, and snippets.

@rickMcGavin
Created September 13, 2016 04:28
Show Gist options
  • Save rickMcGavin/413c187fb8360c53dd3d1e2fee16d098 to your computer and use it in GitHub Desktop.
Save rickMcGavin/413c187fb8360c53dd3d1e2fee16d098 to your computer and use it in GitHub Desktop.
// freecodecamp
// intermediate algorithm scripting
// binary agents
function binaryAgent(str) {
// break the long string of numbers in binary in to an array of strings
var arr = str.split(" ");
// declare 2 empty array
var arr2 = [];
var arr3 = [];
// loop through array converting each element in to a number and pushing it
// in to the empty arr2
arr.forEach(function(e, i){
arr2.push(Number(e));
});
// loop through each element of arr2 converting the binary to
// unicode and then the unicode to String while pushing in to empty arr3
arr2.forEach(function(e, i) {
arr3.push(String.fromCharCode(Number(parseInt(e, 2).toString(10))));
});
// return the 3rd array and join back together as a string
return arr3.join("");
}
console.log(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