-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalg17-binaryAgents.js
More file actions
89 lines (73 loc) · 2.83 KB
/
alg17-binaryAgents.js
File metadata and controls
89 lines (73 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
const assert = require('assert');
/*
Source: <https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/binary-agents>
Binary Agents
Return an English translated sentence of the passed binary string.
The binary string will be space separated.
*/
function myBinaryAgent(str) {
const translatedSentence = str.split(' ')
.map((binary) => String.fromCharCode(parseInt(binary, 2)))
.join('');
return translatedSentence;
}
myBinaryAgent('01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111')
assert.strictEqual(
myBinaryAgent('01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111')
, 'Aren\'t bonfires fun!?'
);
assert.strictEqual(
myBinaryAgent('01001001 00100000 01101100 01101111 01110110 01100101 00100000 01000110 01110010 01100101 01100101 01000011 01101111 01100100 01100101 01000011 01100001 01101101 01110000 00100001')
, 'I love FreeCodeCamp!'
);
/*
Get a help > Get a hint <https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-binary-agents/14273>
*/
//Solution 1
function binaryAgent1(str) {
var biString = str.split(" ");
var 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 (var i = 0; i < biString.length; i++) {
uniString.push(String.fromCharCode(parseInt(biString[i], 2)));
}
// we then simply join the string
return uniString.join("");
}
//Solution 2
function binaryAgent2(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;
}
//Solution 3
function binaryAgent3(str) {
return String.fromCharCode(
...str.split(" ").map(function(char) {
return parseInt(char, 2);
})
);
}
//Solution 4
const binaryAgent4 = str => str.replace(/\d+./g, char => String.fromCharCode(`0b${char}`));