loading...

Solving "Binary Agents" / freeCodeCamp Algorithm Challenges

virenb profile image Viren B Originally published at virenb.cc ・3 min read

Post can also be found on virenb.cc

'Binary Agents'

Let's solve freeCodeCamp's intermediate algorithm scripting challenge, 'Binary Agents'.

Starter Code

function binaryAgent(str) {
  return str;
}

binaryAgent("01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111");

Instructions

Return an English translated sentence of the passed binary string.

The binary string will be space separated.

Test Cases (& Rules)

  • binaryAgent("01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111") should return "Aren't bonfires fun!?"
  • binaryAgent("01001001 00100000 01101100 01101111 01110110 01100101 00100000 01000110 01110010 01100101 01100101 01000011 01101111 01100100 01100101 01000011 01100001 01101101 01110000 00100001") should return "I love FreeCodeCamp!"

Our Approach

After reading the instructions, starter code, and test cases more than once, this is what we're working with:

  • Our function takes in one argument, str, a string. It is a sentence of binary digits, 0's and 1's.
  • We must return a string.
  • We need to convert str to an English sentence.

To begin solving this challenge, let us look at the argument, str. The two test cases have str has long binary sentences. We know that binary strings can be translated to English letters and characters.

We can convert our str string to an array using split(' '). Split() with ' ' will separate the string at each whitespace.

String.split() on MDN

"01000001 01110010 01100101 01101110 00100111 01110100 00100000".split(' ')
// [ "01000001", "01110010", "01100101", "01101110", "00100111", "01110100", "00100000" ]

We now have an array of strings (binary values still). Having an array, we can evaluate each value and figure out how convert the value from binary to English characters. We can opt for a for loop or another method like map().

We can iterate through the array with map(), but how to translate each index? Looking at built-in String methods, one which could be of use is fromCharCode().

MDN: String.fromCharCode()

The static String.fromCharCode() method returns a string created from the specified sequence of UTF-16 code units.

This is useful but would not work yet since our strings are in binary. There is another method, parseInt(), to convert each binary string to decimal.

MDN: parseInt()

parseInt(str, radix)
// radix = 2
parseInt('01000001', 2)
// 65
String.fromCharCode(65)
// A

So we can combine these within our map() function.

str.split(' ') // Creates an array
     .map(b => String.fromCharCode(parseInt(b, 2))) // iterates through the array, converts binary to decimal then returns a character

Here is a little example:

str = "01001000 01001001 00100001";
str.split(' ') // [ "01001000", "01001001", "00100001" ]
str.map(b => String.fromCharCode(parseInt(b, 2)))
// 1. parseInt('01001000', 2) // 72
// 1a. String.fromCharCode(72) // H

// 2. parseInt('01001001', 2) // 73
// 2a. String.fromCharCode(73) // I

// 3. parseInt('00100001', 2) // 33
// 3a. String.fromCharCode(33) // !

The last step is to join back the array into a string.

And return! My solution is to chain everything instead of creating a variable.

Our Solution

function binaryAgent(str) {
  return str.split(' ')
            .map(b => String.fromCharCode(parseInt(b, 2)))
            .join('');
}

Links & Resources

'Binary Agents' Challenge on fCC

freeCodeCamp

Donate to FCC!

Solution on my GitHub

Thank you for reading!

Posted on by:

virenb profile

Viren B

@virenb

Self-taught developer, always looking to learn more.

Discussion

markdown guide
 
def binaryAgent(s): 
    return ''.join(chr(int(x,2)) for x in s.split())