-
Notifications
You must be signed in to change notification settings - Fork 0
/
using-filter.js
61 lines (56 loc) · 1.98 KB
/
using-filter.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
function filterShortStateName(arr) {
// Filter states with less than 7 characters
return arr.filter(function(element) {
return element.length < 7;
});
}
function filterStartVowel(arr) {
// Check if the state starts with a vowel
return arr.filter(function(element) {
let vowels = ['a', 'e', 'i', 'o', 'u'];
for (let i = 0; i < vowels.length; i++) {
if (element[0].toLowerCase() === vowels[i]) {
return true; // Found a vowel at the start
}
}
return false; // No vowel at the start
});
}
function filter5Vowels(arr) {
// Filter states with at least 5 vowels
return arr.filter(function(element) {
let counter = 0;
let vowels = ['a', 'e', 'i', 'o', 'u'];
for (let i = 0; i < element.length; i++) {
for (let j = 0; j < vowels.length; j++) {
if (element[i].toLowerCase() === vowels[j]) {
counter++;
if (counter >= 5) {
return true; // At least 5 vowels found
}
}
}
}
return false; // Less than 5 vowels
});
}
function filter1DistinctVowel(arr) {
return arr.filter(function(element) {
let foundVowels = new Set();
for (let char of element.toLowerCase()) {
if ('aeiou'.includes(char)) {
foundVowels.add(char); // Add the vowel to the set
}
}
return foundVowels.size === 1; // Only one distinct vowel
});
}
function multiFilter(arr) {
return arr.filter(function(element) {
let hasMinCapitalLength = element.capital.length >= 8;
let nameDoesNotStartWithVowel = !'aeiou'.includes(element.name[0].toLowerCase());
let hasVowelInTag = /[aeiou]/i.test(element.tag);
let isNotInSouth = element.region !== "South";
return hasMinCapitalLength && nameDoesNotStartWithVowel && hasVowelInTag && isNotInSouth;
});
}