Skip to content

Commit 8145934

Browse files
committed
Vowels counter
1 parent 11ab8e1 commit 8145934

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ Once inside, run the command below:
88
```npm run test <folder name>```
99

1010
1. String reversal
11+
2. Vowels counter

src/vowelsCounter/index.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// pick a solution and insert here to run the test.
2+
3+
4+
function vowelsCounter(text) {
5+
// Search text with Regex and store all matching instances
6+
let matchingInstances = text.match(/[aeiou]/gi);
7+
8+
// Check if matching instances exist then calculate length
9+
if (matchingInstances) {
10+
// Return number of vowels
11+
return matchingInstances.length
12+
} else {
13+
return 0
14+
}
15+
}
16+
17+
module.exports = vowelsCounter;

src/vowelsCounter/solutions.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// ITERATIVE APPROACH
2+
const vowels = ["a", "e", "i", "o", "u"]
3+
4+
5+
function vowelsCounter(text) {
6+
// Initialize counter
7+
let counter = 0;
8+
9+
10+
// Loop through text to test if each character is a vowel
11+
for (let letter of text.toLowerCase()) {
12+
if (vowels.includes(letter)) {
13+
counter++
14+
}
15+
}
16+
17+
// Return number of vowels
18+
return counter
19+
}
20+
21+
// REGULAR EXPRESSIONS
22+
23+
function vowelsCounter(text) {
24+
// Search text with Regex and store all matching instances
25+
let matchingInstances = text.match(/[aeiou]/gi);
26+
27+
// Check if matching instances exist then calculate length
28+
if (matchingInstances) {
29+
// Return number of vowels
30+
return matchingInstances.length
31+
} else {
32+
return 0
33+
}
34+
}

src/vowelsCounter/test.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const vowelsCounter = require('./index');
2+
3+
test('vowelsCounter is a function', () => {
4+
expect(typeof vowelsCounter).toEqual('function');
5+
});
6+
7+
test('returns the number of vowels found', () => {
8+
expect(vowelsCounter('aeiou')).toEqual(5);
9+
});
10+
11+
test('returns the number of vowels found when string is capitalized', () => {
12+
expect(vowelsCounter('AEIOU')).toEqual(5);
13+
});
14+
15+
test('returns the number of vowels found when all alphabets are passed in', () => {
16+
expect(vowelsCounter('abcdefghijklmnopqrstuvwxyz')).toEqual(5);
17+
});
18+
19+
test('returns the number of vowels found when string has mixed capitalization', () => {
20+
expect(vowelsCounter('Abcdegfizzjbhso')).toEqual(4);
21+
});

0 commit comments

Comments
 (0)