hint [5/7]
Solution ahead!
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [6/7]
function binaryAgent(str) {
biString = str.split(' ');
uniString = [];
/*using the radix (or base) parameter in parseInt, we can convert the binary
number to a decimal number while simultaneously converting to a char*/
for(i=0;i < biString.length;i++){
uniString.push(String.fromCharCode(parseInt(biString[i], 2)));
}
//we then simply join the string
return uniString.join('');
}
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [1/9]
Make sure you've tried to hard to solve it yourself before proceeding. :construction:
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [2/9]
This problem is very straight forward, you will get string that will represent a sentence in binary code, and you need to translate that into words. There is not direct way to do this so you will have to translate twice.
You should first convert from binary to decimal and from decimal before translating those values into characters
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [3/9]
Things are easier when focusing on smaller parts, divide the input to focus on one letter at the time.
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [4/9]
Make sure that each time you transcode a character from binary to decimal, that you reset whatever variable you used to keep track of the ones. Also do not forget to turn everything back into one string.
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [1/10]
Make sure you've tried to hard to solve it yourself before proceeding. :construction:
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [2/10]
This problem is very straight forward, you will get a string that will represent a sentence in binary code, and you need to translate that into words. There is not direct way to do this so you will have to translate twice.
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [3/10]
You should first convert from binary to decimal before translating those values into characters
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [4/10]
Things are easier when focusing on smaller parts, divide the input to focus on one letter at the time.
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [5/10]
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.
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [6/10]
Solution ahead!
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [7/10]
function binaryAgent(str) {
biString = str.split(' ');
uniString = [];
/*using the radix (or base) parameter in parseInt, we can convert the binary
number to a decimal number while simultaneously converting to a char*/
for(i=0;i < biString.length;i++){
uniString.push(String.fromCharCode(parseInt(biString[i], 2)));
}
//we then simply join the string
return uniString.join('');
}
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [8/10]
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.
}
}
// 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;
}
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [9/10]
function binaryAgent(str) {
return String.fromCharCode(...str.split(" ").map(function(char){ return parseInt(char, 2); }));
}
split()
to be able to work on each character as an Array elementmap()
to process each element from binary to decimal using pareseInt()
String.fromCharCode()
to convert each ASCII number into the corresponding characterfromCharCode()
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; https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operatortype
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [10/10]
function binaryAgent(str) {
var re = /(\d+)(\s?)/g;
function convertToChar(match,p1,p2){
return String.fromCharCode(parseInt(p1, 2));
}
return str.replace(re, convertToChar);
}
String.replace()
to find all the binary numbers and convert them to charactersparseInt()
and then into a characterString.fromCharCode()
. By not using the second parenthisized submatch we leave out all the spaces that where in between the binary numbers.String.replace()
function.If you found this page useful, you can give thanks by copying and pasting this on the main chat: thanks @Rafase282 @JamesKee @sabahang @crisvdkooij for your help with Bonfire: Binary Agents
NOTE: Please add your username only if you have added any relevant main contents to the wiki page. (Please don't remove any existing usernames.)
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [1/10]
Make sure you've tried to hard to solve it yourself before proceeding. :construction:
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [2/10]
This problem is very straight forward, you will get a string that will represent a sentence in binary code, and you need to translate that into words. There is not direct way to do this so you will have to translate twice.
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [3/10]
You should first convert from binary to decimal before translating those values into characters
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [4/10]
Things are easier when focusing on smaller parts, divide the input to focus on one letter at the time.
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [5/10]
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.
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [6/10]
Solution ahead!
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [7/10]
function binaryAgent(str) {
biString = str.split(' ');
uniString = [];
/*using the radix (or base) parameter in parseInt, we can convert the binary
number to a decimal number while simultaneously converting to a char*/
for(i=0;i < biString.length;i++){
uniString.push(String.fromCharCode(parseInt(biString[i], 2)));
}
//we then simply join the string
return uniString.join('');
}
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [1/1]
Make sure you've tried to hard to solve it yourself before proceeding. :construction:
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]