-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasic DeNico.js
More file actions
81 lines (66 loc) · 1.99 KB
/
Copy pathBasic DeNico.js
File metadata and controls
81 lines (66 loc) · 1.99 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
/* Task
Write a function deNico/de_nico() that accepts two parameters:
key/$key - string consists of unique letters and digits
message/$message - string with encoded message
and decodes the message using the key.
First create a numeric key basing on the provided key by assigning each letter position in which it is located after setting the letters from key in an alphabetical order.
For example, for the key crazy we will get 23154 because of acryz (sorted letters from the key).
Let's decode cseerntiofarmit on using our crazy key.
1 2 3 4 5
---------
c s e e r
n t i o f
a r m i t
o n
After using the key:
2 3 1 5 4
---------
s e c r e
t i n f o
r m a t i
o n
Notes
The message is never shorter than the key.
Don't forget to remove trailing whitespace after decoding the message
Examples
deNico("crazy", "cseerntiofarmit on ") => "secretinformation"
deNico("abc", "abcd") => "abcd"
deNico("ba", "2143658709") => "1234567890"
deNico("key", "eky") => "key"
Check the test cases for more examples. */
const deNico = (key, m) => {
const memory = []
key
.split('')
.sort()
.forEach((el, index) =>
memory.push({
letter: el,
index: index,
})
)
const numericKey = key
.split('')
.map((el) => memory.findIndex((e) => e.letter === el) + 1)
let formattedM = new Array(Math.ceil(m.length / numericKey.length)).fill('0')
for (let i = 0; i < formattedM.length; i++) {
formattedM[i] = m
.slice(numericKey.length * i, numericKey.length * (i + 1))
.split('')
}
const decodedM = []
formattedM.forEach((array, index) => {
const tempArray = []
numericKey.forEach((num) => {
tempArray.push(array[num - 1])
})
decodedM.push(tempArray)
})
return decodedM.join('').split(',').join('').trimRight()
}
console.log(deNico('crazy', 'cseerntiofarmit on '))
console.log(deNico('abc', 'abcd'))
console.log(deNico('ba', '2143658709'))
console.log(deNico('key', 'eky'))
console.log(deNico('i8', '5 56bn'))
console.log('b'.charCodeAt(0))