Files

177 lines
7.4 KiB
Markdown
Raw Normal View History

2018-10-12 15:37:13 -04:00
---
title: Binary Agents
---
# Binary Agents
2018-10-12 15:37:13 -04:00
---
## Problem Explanation
2018-10-12 15:37:13 -04:00
This problem is very straight forward - you start with a string that represents a sentence in binary code, and you need to translate that into words. There is not a direct way to do this so you will have to translate twice.
2018-10-12 15:37:13 -04:00
#### Relevant Links
2018-10-12 15:37:13 -04:00
* <a href='http://forum.freecodecamp.com/t/javascript-string-prototype-charcodeat/15933' target='_blank' rel='nofollow'>String.prototype.charCodeAt</a>
* <a href='https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCharCode' target='_blank' rel='nofollow'>String.fromCharCode</a>
2018-10-12 15:37:13 -04:00
---
## Hints
### Hint 1
2018-10-12 15:37:13 -04:00
You should first convert from **binary** to **decimal** before translating those values into characters.
2018-10-12 15:37:13 -04:00
### Hint 2
2018-10-12 15:37:13 -04:00
Things are easier when focusing on smaller parts, divide the input to focus on one letter at a time.
2018-10-12 15:37:13 -04:00
### Hint 3
2018-10-12 15:37:13 -04:00
Make sure that each time you transcode a character from binary to decimal, you reset whatever variable you used to keep track of the ones. Also do not forget to turn everything back into one string.
---
## Solutions
2018-10-12 15:37:13 -04:00
<details><summary>Solution 1 (Click to Show/Hide)</summary>
2018-10-12 15:37:13 -04:00
```javascript
function binaryAgent(str) {
var biString = str.split(" ");
var uniString = [];
2018-10-12 15:37:13 -04:00
/*using the radix (or base) parameter in parseInt, we can convert the binary
2018-10-12 15:37:13 -04:00
number to a decimal number while simultaneously converting to a char*/
for (var i = 0; i < biString.length; i++) {
uniString.push(String.fromCharCode(parseInt(biString[i], 2)));
}
2018-10-12 15:37:13 -04:00
// we then simply join the string
return uniString.join("");
}
2018-10-12 15:37:13 -04:00
// test here
binaryAgent(
"01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111"
);
2018-10-12 15:37:13 -04:00
```
#### Code Explanation
2018-10-12 15:37:13 -04:00
* Separate the string into an array of strings separated by whitespace.
* Create some variables that you will use along the way - the names are self explanatory for the most part.
2018-10-12 15:37:13 -04:00
* Iterate through each binary string in the new array.
* Convert to decimal by using `parseInt(_binary_, 2)`. Use the second parameter to specify the base of the input numbers.
* At the end, return the converted message.
2018-10-12 15:37:13 -04:00
#### Relevant Links
2018-10-12 15:37:13 -04:00
* <a href='http://forum.freecodecamp.com/t/javascript-string-prototype-split/15944' target='_blank' rel='nofollow'>String.prototype.split</a>
* <a href='https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt' target='_blank' rel='nofollow'>parseInt</a>
</details>
<details><summary>Solution 2 (Click to Show/Hide)</summary>
2018-10-12 15:37:13 -04:00
```javascript
function binaryAgent(str) {
// Separate the binary code by space.
str = str.split(" ");
var power;
var decValue = 0;
var sentence = "";
// Check each binary number from the array.
for (var s = 0; s < str.length; s++) {
// Check each bit from binary number
for (var t = 0; t < str[s].length; t++) {
// This only takes into consideration the active ones.
if (str[s][t] == 1) {
// This is quivalent to 2 ** position
power = Math.pow(2, +str[s].length - t - 1);
decValue += power;
// Record the decimal value by adding the number to the previous one.
2018-10-12 15:37:13 -04:00
}
}
// After the binary number is converted to decimal, convert it to string and store
sentence += String.fromCharCode(decValue);
// Reset decimal value for next binary number.
decValue = 0;
}
return sentence;
}
// test here
binaryAgent(
"01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111"
);
2018-10-12 15:37:13 -04:00
```
#### Code Explanation
2018-10-12 15:37:13 -04:00
* For each of these binary strings, check for the ones and ignore the zeroes.
* For those that are one or active then convert them to decimal, this takes into account the position and the right power it needs to be raised to.
* Store the power into the **power** variable by adding it to any previous ones on the variable **decValue**. This variable will add and add the powers of the active ones until the end of the loop and then return the decimal number.
* Convert the final decimal outside of the inner loop and then convert it to ASCII and saving it to **sentence** along with any other text string already converted and stored.
* Reset the variable **decValue** to avoid getting wrong decimals before continuing to the outer loop.
#### Relevant Links
2018-10-12 15:37:13 -04:00
* <a href='http://forum.freecodecamp.com/t/javascript-math-pow/14685' target='_blank' rel='nofollow'>Math.pow</a>
* <a href='https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length' target='_blank' rel='nofollow'>String.length</a>
</details>
2018-10-12 15:37:13 -04:00
<details><summary>Solution 3 (Click to Show/Hide)</summary>
2018-10-12 15:37:13 -04:00
```javascript
function binaryAgent(str) {
return String.fromCharCode(
...str.split(" ").map(function(char) {
return parseInt(char, 2);
})
);
}
// test here
binaryAgent(
"01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111"
);
2018-10-12 15:37:13 -04:00
```
#### Code Explanation
2018-10-12 15:37:13 -04:00
* First we use `split()` to be able to work on each character as an Array element
* Then use `map()` to process each element from binary to decimal using `pareseInt()`
* Last we can use `String.fromCharCode()` to convert each ASCII number into the corresponding character
* However `fromCharCode()` expects a series of numbers rather than an Array! We can use ES6 Spread Operator to pass in an Array of numbers as individual numbers. See here for more info; <a href='https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator' target='_blank' rel='nofollow'>Spread Operator</a>
#### Relevant Links
2018-10-12 15:37:13 -04:00
* <a href='http://forum.freecodecamp.com/t/javascript-array-prototype-map/14294' target='_blank' rel='nofollow'>Array.prototype.map</a>
## Alternative Advanced Code Solution:
```js
const binaryAgent = str => str.replace(/\d+./g, char => String.fromCharCode(`0b${char}`));
binaryAgent("01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111");
```
#### Code Explanation
* Find all groups of one or more digits followed by one other character
* Replace with a string created from the specified sequence of UTF-16 code units
* Use `0b` to lead the code unit to express that a binary integer literal is being converted.
#### Relevant Links
* <a href='https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCharCode' target='_blank' rel='nofollow'>String.fromCharCode()</a>
* <a href='https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace' target='_blank' rel='nofollow'>String.prototype.replace()</a>
* <a href='https://codegolf.stackexchange.com/questions/35096/convert-a-string-of-binary-characters-to-the-ascii-equivalents' target='_blank' rel='nofollow'>Convert a string of binary characters to the ASCII equivalents</a>
* <a href='https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Grammar_and_types#Numeric_literals' target='_blank' rel='nofollow'>Grammar and types/Numeric Literals</a>
</details>