Skip to content

Commit c30b897

Browse files
merge: count Vowels (TheAlgorithms#864)
1 parent 4d55b40 commit c30b897

File tree

3 files changed

+48
-44
lines changed

3 files changed

+48
-44
lines changed

String/CheckVowels.js

Lines changed: 0 additions & 21 deletions
This file was deleted.

String/CountVowels.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @function countVowels
3+
* @description Given a string of words or phrases, count the number of vowels.
4+
* @param {String} url - The input string
5+
* @return {Number} count
6+
* @example countVowels("ABCDE") => 2
7+
* @example countVowels("Hello") => 2
8+
*/
9+
10+
const countVowels = (str) => {
11+
if (typeof str !== 'string') {
12+
throw new TypeError('Input should be a string')
13+
}
14+
const vowels = new Set(['a', 'e', 'i', 'o', 'u'])
15+
let count = 0
16+
for (let i = 0; i < str.length; i++) {
17+
const char = str[i].toLowerCase()
18+
if (vowels.has(char)) {
19+
count++
20+
}
21+
}
22+
return count
23+
}
24+
25+
export { countVowels }
Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,67 @@
1-
import { checkVowels } from '../CheckVowels'
1+
import { countVowels } from '../CountVowels'
22

3-
describe('Test the checkVowels function', () => {
3+
describe('CountVowels', () => {
44
it('expect throws on use wrong param', () => {
5-
expect(() => checkVowels(0)).toThrow()
5+
expect(() => countVowels(0)).toThrow()
66
})
77

88
it('count the vowels in a string', () => {
99
const value = 'Mad World'
10-
const countVowels = checkVowels(value)
11-
expect(countVowels).toBe(2)
10+
const count = countVowels(value)
11+
expect(count).toBe(2)
1212
})
1313

1414
it('should return 0 when input is a string with no vowels', () => {
1515
const value = 'bcdfgh'
16-
const countVowels = checkVowels(value)
17-
expect(countVowels).toBe(0)
16+
const count = countVowels(value)
17+
expect(count).toBe(0)
1818
})
1919

2020
it('should return 1 when input is a string of length 1 that is a vowel', () => {
2121
const value = 'a'
22-
const countVowels = checkVowels(value)
23-
expect(countVowels).toBe(1)
22+
const count = countVowels(value)
23+
expect(count).toBe(1)
2424
})
2525

2626
it('should return the correct result when input is in all uppercase letters', () => {
2727
const value = 'ABCDE'
28-
const countVowels = checkVowels(value)
29-
expect(countVowels).toBe(2)
28+
const count = countVowels(value)
29+
expect(count).toBe(2)
3030
})
3131

3232
it('should return the correct result when input is in all lowercase letters', () => {
3333
const value = 'abcdefghi'
34-
const countVowels = checkVowels(value)
35-
expect(countVowels).toBe(3)
34+
const count = countVowels(value)
35+
expect(count).toBe(3)
3636
})
3737

3838
it('should return the correct result when input string contains spaces', () => {
3939
const value = 'abc def ghi'
40-
const countVowels = checkVowels(value)
41-
expect(countVowels).toBe(3)
40+
const count = countVowels(value)
41+
expect(count).toBe(3)
4242
})
4343

4444
it('should return the correct result when input contains number characters', () => {
4545
const value = 'a1b2c3'
46-
const countVowels = checkVowels(value)
47-
expect(countVowels).toBe(1)
46+
const count = countVowels(value)
47+
expect(count).toBe(1)
4848
})
4949

5050
it('should return the correct result when input contains punctuation characters', () => {
5151
const value = 'a!b.ce)'
52-
const countVowels = checkVowels(value)
53-
expect(countVowels).toBe(2)
52+
const count = countVowels(value)
53+
expect(count).toBe(2)
5454
})
5555

5656
it('should return 0 when the input is an empty string', () => {
5757
const value = ''
58-
const countVowels = checkVowels(value)
59-
expect(countVowels).toBe(0)
58+
const count = countVowels(value)
59+
expect(count).toBe(0)
6060
})
6161

6262
it('should count multiple occurrences of the same vowel in the input', () => {
6363
const value = 'aaaaa'
64-
const countVowels = checkVowels(value)
65-
expect(countVowels).toBe(5)
64+
const count = countVowels(value)
65+
expect(count).toBe(5)
6666
})
6767
})

0 commit comments

Comments
 (0)