-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwhere-my-anagram.js
65 lines (57 loc) · 1.49 KB
/
where-my-anagram.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
// where my anagram at?
function anagrams(word, words) {
let main = [];
let arr = [];
let mainArr = [];
for (let i = 0; i < words.length; i++) {
if (word.length === words[i].length) main.push([...words[i]]);
}
for (let i = 0; i < main.length; i++) {
let tf;
let curr = main[i];
for (let i = 0; i < curr.length; i++) {
if (word.includes(curr[i])) {
tf = true;
} else {
tf = false;
break;
}
}
if (tf) {
arr.push(curr);
}
}
let curr = " ";
for (let i = 0; i < arr.length; i++) {
let tf;
let cur = arr[i];
for (let j = 0; j < word.length; j++) {
if (cur.includes(word[j])) {
tf = true;
} else {
tf = false;
break;
}
}
if (tf) {
curr += cur;
}
}
let lastMainWord = curr
.split("")
.filter((e) => e !== ",")
.join("");
let lw = chunkSubstr(lastMainWord.replace(" ", ""), word.length);
function chunkSubstr(str, size) {
const numChunks = Math.ceil(str.length / size);
const chunks = new Array(numChunks);
for (let i = 0, o = 0; i < numChunks; ++i, o += size) {
chunks[i] = str.substr(o, size);
}
return chunks;
}
return lw;
}
anagrams("abba", ["aabb", "abcd", "bbaa", "dada"]); //['aabb', 'bbaa']
anagrams("laser", ["lazing", "lazy", "lacer"]); //[];
anagrams("racer", ["crazer", "carer", "racar", "caers", "racer"]); //['carer', 'racer']