-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
which-are-in.js
65 lines (58 loc) · 1.52 KB
/
which-are-in.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
function inArray(array1,array2){
// a place to store the results
const results = [];
// iterate over array2
array2.forEach(word => {
// iterate over array1
array1.forEach(ending => {
// if the current word ends with something in array1
if (word.includes(ending)) {
// push into results
results.push(ending);
}
})
});
return [...new Set(results)].sort();
}
function inArray(array1,array2){
const results = [];
array2.forEach(word => {
array1.forEach((ending, i) => {
if (word.includes(ending)) {
array1.splice(i, 1);
results.push(ending);
}
})
});
return results.sort();
}
function inArray(array1,array2){
const results = [];
array2.forEach(word => {
for (let i = array1.length - 1; i >= 0; i--) {
const ending = array1[i];
if (word.includes(ending)) {
array1.splice(i, 1);
results.push(ending);
break;
}
}
});
return results.sort();
}
function inArray(array1,array2){
return array1.filter(ending => {
return array2.some(word => word.includes(ending));
}).sort();
}
// Alca???
// function inArray(array1,array2) {
// return array2.filter(ending => new RegExp(array1.join('|')).test(n))
// }
let a1 = ["xyz", "live", "strong"]
let a2 = ["lively", "alive", "harp", "sharp", "armstrong"]
console.log(inArray(a1, a2), ["live", "strong"])
a1 = ["live", "strong", "arp"]
console.log(inArray(a1, a2), ["arp", "live", "strong"])
a1 = ["tarp", "mice", "bull"]
console.log(inArray(a1, a2), [])