-
Notifications
You must be signed in to change notification settings - Fork 13
/
1a2b.core.js
91 lines (67 loc) · 1.79 KB
/
1a2b.core.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
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
90
91
'use strict';
const GraphemeSplitter = require('grapheme-splitter');
const splitter = new GraphemeSplitter();
const length = (str) => {
const arr = splitter.splitGraphemes(str);
return arr.length;
};
const shuffle = (str, total) => {
const arr = splitter.splitGraphemes(str);
let result = '';
for (let i = 0; i < total; i += 1) {
const j = Math.floor(Math.random() * arr.length);
result += arr[j];
arr[j] = arr[arr.length - 1];
arr.pop();
}
return result;
};
const extraChar = (str, str2) => {
const arr = splitter.splitGraphemes(str);
const arr2 = splitter.splitGraphemes(str2);
for (const i in arr2) {
const j = arr.indexOf(arr2[i]);
if (j >= 0) {
arr[j] = arr[arr.length - 1];
arr.pop();
}
}
return arr.length;
};
const reveal = (str, strH, strC) => {
const arr = splitter.splitGraphemes(str);
const arrH = splitter.splitGraphemes(strH);
const arrC = splitter.splitGraphemes(strC);
for (const i in arrC) {
const j = arr.indexOf(arrC[i]);
if (j >= 0) {
arrH[i] = arrC[i];
arr[j] = arr[arr.length - 1];
arr.pop();
}
}
return arrH.join('');
};
const getA = (str, str2) => {
const arr = splitter.splitGraphemes(str);
const arr2 = splitter.splitGraphemes(str2);
let result = 0;
for (const i in arr2) {
if (arr2[i] === arr[i]) {
result += 1;
}
}
return result;
};
const getAB = (str, str2) => {
const valA = getA(str, str2);
const valB = length(str) - extraChar(str, str2) - valA;
return [valA, valB];
};
module.exports = {
length: length,
shuffle: shuffle,
extraChar: extraChar,
reveal: reveal,
getAB: getAB,
};