-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountVowels.js
More file actions
114 lines (93 loc) · 2.54 KB
/
countVowels.js
File metadata and controls
114 lines (93 loc) · 2.54 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// taking in a string
// find out how many vowels are in that string
// return # of vowels that we get from that string
// the word "why" would return 0 vowels
// the word hello should return two vowels
// input the string "Tuesday" : 3
// return the sum of all the vowels contained in the string
// split method turns each character from a string into an array
// loop over the string characters array
// crosss reference vowels(a, e, i, o, u) to the string input using a loop on array || regex || object
// comparing each character to the vowels
// Increment if they match
// return the count
function findVowels(str) {
// With arrays
let strArray = str.toLowerCase().split('');
let vowels = ['a', 'e', 'i', 'o', 'u'];
let vowelCount = 0;
for (let i = 0; i < strArray.length; i++) {
for (let j = 0; j < vowels.length; j++) {
if (strArray[i] === vowels[j]) {
vowelCount++;
}
}
}
return vowelCount;
}
// test if the character (strArray[i]) is in the vowels regex
function findVowels(str) {
// With Regular Expression test
let strArray = str.toLowerCase().split('');
let vowels = /[aeiou]/;
let vowelCount = 0;
for (let i = 0; i < strArray.length; i++) {
if (vowels.test(strArray[i])) {
vowelCount++;
}
}
return vowelCount;
}
// test if the character (strArray[i]) is in the vowels object
function findVowels(str) {
// With Regular Expression test
let strArray = str.toLowerCase().split('');
let vowels = {
a: '', //better to use truthy instead of falsy like having a: true instead of ''
e: '',
i: '',
o: '',
u: ''
};
let vowelCount = 0;
for (let i = 0; i < strArray.length; i++) {
if (vowels[strArray[i]]) {
vowelCount++;
}
}
return vowelCount;
}
console.log(findVowels('why'));
console.log(findVowels('how many vowels are in that string'));
//USING INCLUDES
function findVowel(str) {
let vowel = 'aeiouAEIOU';
let count = 0
for (let char of str) {
if (vowel.includes(char)) {
count++
}
}
return count;
}
console.log(findVowel('daniel') )
//TO LOWERCASE()
function findVowels(str) {
let vowel = 'aeiou';
let count = 0;
for (let char of str) {
if (vowel.includes(char.toLowerCase())) {
count++;
}
}
return count;
}
//USING INCLUDES WITH REDUCE
function findVowels(str) {
const vowels = 'aeiou';
return str
.split('')
.reduce((count, char) => vowels.includes(char) ? count + 1 : count, 0);
}
//REGEX (MATCH)
const findVowels = str => (str.match(/[aeiou]/g) || []).length;