Skip to content

Commit 494f4c2

Browse files
committed
--update : counting vowels
1 parent 8a2dd9c commit 494f4c2

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

src/count-vowels/count-vowels.test.js

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
const { countVowelsItteratively, countVowelsItterativelyES6, countVowelsUsingRegex } = require('.');
2+
3+
describe('Count Vowels', () => {
4+
const apple = 'AppLe';
5+
const education = 'education';
6+
const myths = 'myths';
7+
8+
describe('Count by regular itteration', () => {
9+
it('Should return 2 for `Apple`', () => {
10+
expect(countVowelsItteratively(apple)).toEqual(2);
11+
});
12+
13+
it('Should return 5 for `Education`', () => {
14+
expect(countVowelsItteratively(education)).toEqual(5);
15+
});
16+
17+
it('Should return 0 for `Myths`', () => {
18+
expect(countVowelsItteratively(myths)).toEqual(0);
19+
});
20+
});
21+
22+
describe('Count by ES6 itteration', () => {
23+
it('Should return 2 for `Apple`', () => {
24+
expect(countVowelsItterativelyES6(apple)).toEqual(2);
25+
});
26+
27+
it('Should return 5 for `Education`', () => {
28+
expect(countVowelsItterativelyES6(education)).toEqual(5);
29+
});
30+
31+
it('Should return 0 for `Myths`', () => {
32+
expect(countVowelsItterativelyES6(myths)).toEqual(0);
33+
});
34+
});
35+
36+
describe('Count using REGEX', () => {
37+
it('Should return 2 for `Apple`', () => {
38+
expect(countVowelsUsingRegex(apple)).toEqual(2);
39+
});
40+
41+
it('Should return 5 for `Education`', () => {
42+
expect(countVowelsUsingRegex(education)).toEqual(5);
43+
});
44+
45+
it('Should return 0 for `Myths`', () => {
46+
expect(countVowelsUsingRegex(myths)).toEqual(0);
47+
});
48+
});
49+
});

src/count-vowels/index.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const pattern = /[^\w]/g;
2+
3+
const cleanString = str => str.replace(pattern, '').toLowerCase();
4+
const isVowel = char => char === 'a' || char === 'e' || char === 'i' || char === 'o' || char === 'u';
5+
6+
function countVowelsItteratively(str) {
7+
const cleanedString = cleanString(str);
8+
let count = 0;
9+
for (let i = 0; i < cleanedString.length; i += 1) {
10+
if (isVowel(cleanedString[i])) {
11+
count += 1;
12+
}
13+
}
14+
return count;
15+
}
16+
17+
function countVowelsItterativelyES6(str) {
18+
const cleanedString = cleanString(str);
19+
const vowels = ['a', 'e', 'i', 'o', 'u'];
20+
let count = 0;
21+
22+
// eslint-disable-next-line no-restricted-syntax
23+
for (const char of cleanedString) {
24+
if (vowels.includes(char)) {
25+
count += 1;
26+
}
27+
}
28+
return count;
29+
}
30+
31+
function countVowelsUsingRegex(str) {
32+
const match = str.match(/[aeiou]/gi);
33+
return match ? match.length : 0;
34+
}
35+
36+
module.exports = {
37+
countVowelsItteratively,
38+
countVowelsItterativelyES6,
39+
countVowelsUsingRegex,
40+
};

0 commit comments

Comments
 (0)