Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Solution to freecodecamp's one of Intermediate Algorithm scripting challenges (Binary Agents) #368

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions Algorithms/Javascript/binary-agents.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,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)