Solving "Binary Agents" / freeCodeCamp Algorithm Challenges
Tags: freecodecamp, algorithms, challenge, javascript
Published: 2020-08-03 21:01:59

---
title: Solving "Binary Agents" / freeCodeCamp Algorithm Challenges
published: true
description: My guide, notes, and solution to freeCodeCamp's intermediate algorithm challenge, "Binary Agents" 
tags: #freeCodeCamp, #algorithms, #challenge, #javascript
canonical_url: https://virenb.cc/fcc-033-binary-agents
series: Solving freeCodeCamp's Algorithm Challenges
---

*Post can also be found on [virenb.cc](https://virenb.cc/fcc-033-binary-agents "virenb post")*

!['Binary Agents'](//images.ctfassets.net/yzwpq1epaq68/3QLIJ0bPtpSXxAhGj1P7vM/968a9688e602f0876313634a68f620bb/ss-033.png)

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



### Starter Code

```javascript
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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split "MDN docs: split()")



```javascript
"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()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCharCode "MDN docs: 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()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt "MDN Docs: parseInt()")



```javascript
parseInt(str, radix)
// radix = 2
```



```javascript
parseInt('01000001', 2)
// 65
```



```javascript
String.fromCharCode(65)
// A
```



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



```javascript
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:



```javascript
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

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



# Links & Resources

['Binary Agents' Challenge on fCC](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/binary-agents "Intermediate Algorithm Scripting: Binary Agents on fCC Website")

[freeCodeCamp](https://www.freecodecamp.org/, "freeCodeCamp")

[Donate to FCC!](https://www.freecodecamp.org/donate "fCC Donation Page")

[Solution on my GitHub](https://github.com/virenb/fcc-algorithms/blob/master/033---binary-agents.js "GitHub Solution")



Thank you for reading!