Remember that when dealing with strings, you can never mutate the original string. That means that you have to declare a new variable and assign an empty string to it. But I had to figure out what method(s) I should use to translate the binary strings into English sentences. It was definitely going to take more than one to achieve this.

The method that immediately came to mind (of course), was String.fromCharCode(). But what was I going to use in conjunction with it to make for the clearest, and most succinct code possible? I didn’t want to create spaghetti code! This took some research.

After much searching, I came across the .map() method. What does the .map() method do? It creates a new array with the results of calling a provided function on every element in this array.

.map() Syntax:

JavaScript

1

arr.map(callback[,thisArg])

Parameters:

JavaScript

1

2

3

4

5

6

7

callback:Functionthat produces an element of the newArray,taking three arguments:

currentValue:The current element being processed inthe array.

index:The index of the current element being processed inthe array.

array:The arraymap was called upon.

Description:

JavaScript

1

2

3

4

5

6

7

map callsaprovided callback functiononce foreachelement inan array,inorder,andconstructsanewarrayfrom the results.callback isinvoked only forindexes of the arraywhich have assigned values,including undefined.It isnotcalled formissing&nbsp;elements of the array&nbsp;(that is,indexes that have never been set,&nbsp;which have been deleted orwhich have never been assignedavalue).

callback isinvoked with three arguments:the value of the element,the index of the element,andthe Arrayobjectbeing traversed.

map does notmutate the arrayon which it iscalled(although callback,ifinvoked,may doso).

The range of elements processed by map isset before the first invocation of callback.Elements which are appended tothe arrayafter the call tomap begins will notbe visited by callback.Ifexisting elements of the arrayare changed,ordeleted,their value aspassed tocallback will be the value at the time map visits them;elements that are deleted are notvisited.

The problem here is that .map() deals with arrays and not strings! So how was I going to fix that problem? With str.split()! str.split() splits a String object into an array of strings by separating the string into substrings. And since I was dealing with individual binary characters, I needed to create a variable that would be the equivalent of the binary character’s English translation. So this is what my code looked like up to this point:

JavaScript

1

2

3

4

5

6

7

functionbinaryAgent(str){

"use strict";

varbinString=' ';

str.split(' ').map(function(bin){}

}

Since str.split(‘ ‘) splits a String object (str) into an array of strings by separating the string into substrings (the individual characters that make up the string), it meant that I was able to use the .map() method. The .map() method is great because it eliminates the need for for loops and therefore less code. This means a lower cyclomatic complexity as well. But then I had to create an English translation equivalent of the individual binary character. That’s where the bin parameter comes in. bin represents the binary character that has to be translated into its English equivalent. It also needs to somehow be pushed into the new binString variable consisting of an empty string. So this is what I did:

JavaScript

1

2

3

4

5

6

7

8

9

10

11

12

13

functionbinaryAgent(str){

"use strict";

varbinString=' ';

str.split(' ').map(function(bin){

binString+=String.fromCharCode(parseInt(bin,2));

}

}

I am using += assignment operator, which means that I am adding String.fromCharCode(parseInt(bin, 2) to binString, because I first have to translate the binary characters before I add them to the new binString. So I am working right to left. Let’s examine how the bin character is translated into its English equivalent:

string: The value to parse. If string is not a string, then it is converted to a string. Leading whitespace (spaces) is ignored.

radix: An integer between 2 and 36 that represents the radix (the base in mathematical numeral systems) of the above mentioned string. Always specify this parameter to eliminate reader confusion and to guarantee predictable behavior. Different implementations produce different results when a radix is not specified, usually defaulting the value to 10.

First the .parseInt() method converts each binary character that has been transformed into a substring that is part of the array of strings created by str.split(‘ ‘), back into a string. Then the radix converts the binary characters into integers that String.fromCharCode() converts to letters of the English alphabet and/or non-alphanumeric characters (such as exclamation points or question marks, etc). I honestly had to guess which radix contributed to the correct conversion. Luckily I started from the lowest number 2, and found that to be the correct radix!

Lastly, binString had to be returned in order for the body of the binaryAgent() function to be accessible to its invocation outside of its local scope.

Share this:

Like this:

Related

Maria Campbell

Founder, Inter-Global Media Network, Inc., where I focus on Front End Development. as well as a digital photographer, videographer, blogger, broadcaster, and graphic designer. Genesis Framework user. I got my start 8+ years ago teaching myself HTML/CSS and Wordpress Development because I couldn't find anyone who could create what I wanted. I have been working with open source software ever since. I am an open source software evangelist, and a "Women in Technology" evangelist. A creative professional myself, I find Web Development to be both practical and creative. I love to cook, travel, read mysteries, love everything French, the beach at sunrise, my three cats, and hope to revisit the city of San Francisco one day.

Reader Interactions

Comment Policy: Your words are your own, so be nice and helpful if you can. Please, only use your real name and limit the amount of links submitted in your comment. We accept clean XHTML in comments, but don't overdo it please. Comments are moderated, so spammy or malicious links will be removed.