-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimple_Encryption_1-Alternate_Split.js
More file actions
59 lines (56 loc) · 1.81 KB
/
Copy pathSimple_Encryption_1-Alternate_Split.js
File metadata and controls
59 lines (56 loc) · 1.81 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
/*
This is my solution the Codewars Challenge: Simple Encryption #1- Alternative Split
You can find my initial attempt at this problem in Python in my "Codewars" repository. I had given up after trying hard to solve
this problem in Python 4 months ago. That file is called "Failed-SimpleEncryption.py".
These two functions encrypt and decrypt a given string.
For building the encrypted string:
Take every 2nd character from the string, then the other characters, that are not every 2nd char, and concatenate them as new String.
Do this n times!
The decrypt function will then decrypt the encrypted text back to its original form.
*/
function encrypt(text, n) {
if (text === null) {return null}
if (n <= 0) {return text}
let secondChars = [];
let otherChars = [];
let i;
let t;
let encryptedText = text;
for (t = 0; t < n; t++) {
secondChars = [];
otherChars = [];
for (i = 0; i < encryptedText.length; i++) {
if (i % 2 === 0) {
secondChars.push(encryptedText[i]);
} else {
otherChars.push(encryptedText[i]);
}
}
encryptedText = (otherChars.concat(secondChars)).join('');
}
return encryptedText;
}
function decrypt(encryptedText, n) {
if (encryptedText === null) {return null}
if (n <= 0) {return encryptedText}
let secondChars = [];
let otherChars = [];
let i;
let t;
let decryptedText = [];
let newText = encryptedText.split('');
for (t = 0; t < n; t++) {
decryptedText = [];
secondChars = newText.slice(newText.length/2, newText.length);
otherChars = newText.slice(0, newText.length/2);
for (i = 0; i < newText.length; i++) {
if (i % 2 === 0) {
decryptedText.push(secondChars.shift());
} else {
decryptedText.push(otherChars.shift());
}
}
newText = decryptedText;
}
return newText.join('');
}