Skip to content

Commit 7a0f016

Browse files
feat: find the vowels algorithm
1 parent 3269e78 commit 7a0f016

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { describe, test } from 'vitest';
2+
import { vowels, vowelsWithRegex } from '../vowels';
3+
4+
describe('Finding Vowels', () => {
5+
test('Vowels is a function', () => {
6+
expect(typeof vowels).toEqual('function');
7+
expect(typeof vowelsWithRegex).toEqual('function');
8+
});
9+
10+
test('returns the number of vowels used', () => {
11+
expect(vowels('aeiou')).toEqual(5);
12+
expect(vowels('abcdefghijklmnopqrstuvwxyz')).toEqual(5);
13+
expect(vowels('bcdfghjkl')).toEqual(0);
14+
});
15+
16+
test('returns with recursion the number of vowels used', () => {
17+
expect(vowelsWithRegex('aeiou')).toEqual(5);
18+
expect(vowelsWithRegex('abcdefghijklmnopqrstuvwxyz')).toEqual(5);
19+
expect(vowelsWithRegex('bcdfghjkl')).toEqual(0);
20+
});
21+
});

src/algorithms/vowels/vowels.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Option 1
2+
export const vowels = (str: string) => {
3+
let count = 0;
4+
const checker = ['a', 'e', 'i', 'o', 'u'];
5+
6+
for (const char of str.toLowerCase()) {
7+
if (checker.includes(char)) {
8+
count++;
9+
}
10+
}
11+
12+
return count;
13+
};
14+
15+
// Option 2
16+
export const vowelsWithRegex = (str: string) => {
17+
const matches = str.match(/[aeiou]/gi);
18+
return matches ? matches.length : 0;
19+
};

0 commit comments

Comments
 (0)