Skip to content

Commit 2d4d9a7

Browse files
feat: add count vowels algorithm
1 parent 5c39e87 commit 2d4d9a7

2 files changed

Lines changed: 30 additions & 60 deletions

File tree

‎String/CountVowels.js‎

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
/**
22
* @function countVowels
3-
* @description Given a string of words or phrases, count the number of vowels.
4-
* @param {String} str - The input string
5-
* @return {Number} - The number of vowels
6-
* @example countVowels("ABCDE") => 2
7-
* @example countVowels("Hello") => 2
3+
* @description Counts the number of vowels (a, e, i, o, u) in a string,
4+
* ignoring case.
5+
* @param {string} str - The input string
6+
* @returns {number} - The number of vowels in the string
7+
* @see https://en.wikipedia.org/wiki/Vowel
8+
* @example countVowels("hello") => 2
9+
* @example countVowels("rhythm") => 0
10+
* @example countVowels("AEIOU") => 5
811
*/
9-
1012
const countVowels = (str) => {
1113
if (typeof str !== 'string') {
12-
throw new TypeError('Input should be a string')
14+
throw new TypeError('Input must be a string')
1315
}
14-
15-
const vowelRegex = /[aeiou]/gi
16-
const vowelsArray = str.match(vowelRegex) || []
17-
18-
return vowelsArray.length
16+
return (str.match(/[aeiou]/gi) || []).length
1917
}
2018

2119
export { countVowels }

‎String/test/CountVowels.test.js‎

Lines changed: 20 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,39 @@
1-
import { countVowels } from '../CountVowels'
1+
import { countVowels } from '../CountVowels.js'
22

3-
describe('CountVowels', () => {
4-
it('expect throws on use wrong param', () => {
5-
expect(() => countVowels(0)).toThrow()
3+
describe('countVowels', () => {
4+
test("countVowels('hello') returns 2", () => {
5+
expect(countVowels('hello')).toBe(2)
66
})
77

8-
it('count the vowels in a string', () => {
9-
const value = 'Mad World'
10-
const count = countVowels(value)
11-
expect(count).toBe(2)
8+
test("countVowels('rhythm') returns 0", () => {
9+
expect(countVowels('rhythm')).toBe(0)
1210
})
1311

14-
it('should return 0 when input is a string with no vowels', () => {
15-
const value = 'bcdfgh'
16-
const count = countVowels(value)
17-
expect(count).toBe(0)
12+
test("countVowels('AEIOU') returns 5", () => {
13+
expect(countVowels('AEIOU')).toBe(5)
1814
})
1915

20-
it('should return 1 when input is a string of length 1 that is a vowel', () => {
21-
const value = 'a'
22-
const count = countVowels(value)
23-
expect(count).toBe(1)
16+
test("countVowels('') returns 0", () => {
17+
expect(countVowels('')).toBe(0)
2418
})
2519

26-
it('should return the correct result when input is in all uppercase letters', () => {
27-
const value = 'ABCDE'
28-
const count = countVowels(value)
29-
expect(count).toBe(2)
20+
test("countVowels('The quick brown fox') returns 5", () => {
21+
expect(countVowels('The quick brown fox')).toBe(5)
3022
})
3123

32-
it('should return the correct result when input is in all lowercase letters', () => {
33-
const value = 'abcdefghi'
34-
const count = countVowels(value)
35-
expect(count).toBe(3)
24+
test('countVowels ignores non-letter characters', () => {
25+
expect(countVowels('a1e2i3o4u5')).toBe(5)
3626
})
3727

38-
it('should return the correct result when input string contains spaces', () => {
39-
const value = 'abc def ghi'
40-
const count = countVowels(value)
41-
expect(count).toBe(3)
28+
test("countVowels('bcdfg') returns 0", () => {
29+
expect(countVowels('bcdfg')).toBe(0)
4230
})
4331

44-
it('should return the correct result when input contains number characters', () => {
45-
const value = 'a1b2c3'
46-
const count = countVowels(value)
47-
expect(count).toBe(1)
32+
test('throws TypeError for non-string input', () => {
33+
expect(() => countVowels(123)).toThrow(TypeError)
4834
})
4935

50-
it('should return the correct result when input contains punctuation characters', () => {
51-
const value = 'a!b.ce)'
52-
const count = countVowels(value)
53-
expect(count).toBe(2)
54-
})
55-
56-
it('should return 0 when the input is an empty string', () => {
57-
const value = ''
58-
const count = countVowels(value)
59-
expect(count).toBe(0)
60-
})
61-
62-
it('should count multiple occurrences of the same vowel in the input', () => {
63-
const value = 'aaaaa'
64-
const count = countVowels(value)
65-
expect(count).toBe(5)
36+
test('throws TypeError for null input', () => {
37+
expect(() => countVowels(null)).toThrow(TypeError)
6638
})
6739
})

0 commit comments

Comments
 (0)