-
Notifications
You must be signed in to change notification settings - Fork 0
/
binaries.js
54 lines (43 loc) · 1004 Bytes
/
binaries.js
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
function code(strng) {
let encodedResult = ''
let stringArray = strng.split('')
for (let num of stringArray) {
encodedResult += conversionTable[num]
}
return encodedResult
}
function decode(str) {
let decodedResult = '';
let tempStr = '';
for (let char of str) {
tempStr += char;
// Check if tempStr matches any value in the conversion table
for (let [key, value] of Object.entries(conversionTable)) {
if (tempStr === value) {
decodedResult += key;
tempStr = ''; // Reset tempStr after a successful match
break; // Exit the loop once a match is found
}
}
}
return decodedResult;
}
let conversionTable = {
'0': '10',
'1': '11',
'2': '0110',
'3': '0111',
'4': '001100',
'5': '001101',
'6': '001110',
'7': '001111',
'8': '00011000',
'9': '00011001'
};
//psuedocode
/*
Turn input string into an array of numbers
encode each number
convert each num to string
concactenate them and return
*/