-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinary-agents.js
More file actions
27 lines (22 loc) · 867 Bytes
/
Binary-agents.js
File metadata and controls
27 lines (22 loc) · 867 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
function binaryAgent(str) {
//split string into arrays
str = str.split(' ');
var binToDec = 0;
var tempArr = [];
//loop through each array element
for (var i = 0; i < str.length; i++) {
binToDec = 0;
//loop through letters of array 'words'
for (var j = str[i].length - 1, pow=0; j >= 0, pow<str[i].length-1; j--, pow++) {
//binary to decimal
binToDec += Math.pow(2, pow) * str[i][j];
}
//Treat decimal numbers as ASCII codes, convert them to letters and push the letters in an array
tempArr.push(String.fromCharCode(binToDec));
}
//join the array elements to create a string.
return tempArr.join("");
}
binaryAgent(
'01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111'
);