-
Notifications
You must be signed in to change notification settings - Fork 179
Expand file tree
/
Copy pathbinary-agents.js
More file actions
24 lines (21 loc) · 888 Bytes
/
binary-agents.js
File metadata and controls
24 lines (21 loc) · 888 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
// Takes a space separated binary string
// Return an English translated sentence of the passed binary string
function binaryAgent(str) {
let convertedString = ""
let binaryArray = str.split(" ")
for (let i = 0; i < binaryArray.length; i++) {
let asciiCode = 0
let positionFromLeft = 0
for (let j = binaryArray[i].length - 1; j >= 0; j--) {
if (binaryArray[i][j] === "1") {
asciiCode += 2 ** positionFromLeft
}
positionFromLeft++;
}
convertedString += String.fromCharCode(asciiCode)
}
return convertedString
}
// Sample
// const result = binaryAgent("01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111");
// console.log(result)