๐Ÿ“ฆ malash / codewars-solutions

๐Ÿ“„ simple-encryption-number-1-alternating-split.js ยท 51 lines
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
51const encryptOnce = chs => {
  const left = [];
  const right = [];
  for (const [index, ch] of chs.entries()) {
    if (index % 2 === 1) {
      left.push(ch);
    } else {
      right.push(ch);
    }
  }
  return left.concat(right);
}

function encrypt(text, n) {
  if (typeof text !== 'string') {
    return text;
  }
  let result = Array.from(text);
  for (let i = 0; i < n; i++) {
    result = encryptOnce(result);
  }
  return result.join('');
}

const decrypttOnce = chs => {
  const mid = parseInt(chs.length / 2, 10);
  const left = chs.slice(0, mid);
  const right = chs.slice(mid, chs.length);
  const result = [];
  while (left.length || right.length) {
    if (right.length) {
      result.push(right.shift());
    }
    if (left.length) {
      result.push(left.shift());
    }
  }
  return result;
}

function decrypt(encryptedText, n) {
  if (typeof encryptedText !== 'string') {
    return encryptedText;
  }
  let result = Array.from(encryptedText);
  for (let i = 0; i < n; i++) {
    result = decrypttOnce(result);
  }
  return result.join('');
}