Skip to content

Commit de65d53

Browse files
Added new algoritm (#433)
1 parent e833b3f commit de65d53

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

String/CheckVowels.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
Given a string of words or phrases, count the number of vowels.
3+
Example: input = "hello world" return 3
4+
*/
5+
6+
const checkVowels = (value) => {
7+
if (typeof value !== 'string') {
8+
throw new TypeError('The first param should be a string')
9+
}
10+
const vowels = ['a', 'e', 'i', 'o', 'u']
11+
let countVowels = 0
12+
for (let i = 0; i < value.length; i++) {
13+
const char = value[i].toLowerCase()
14+
if (vowels.includes(char)) {
15+
countVowels++
16+
}
17+
}
18+
return countVowels
19+
}
20+
21+
export { checkVowels }

String/CheckVowels.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { checkVowels } from './CheckVowels'
2+
3+
describe('Test the checkVowels function', () => {
4+
it('expect throws on use wrong param', () => {
5+
expect(() => checkVowels(0)).toThrow()
6+
})
7+
it('count the vowels in a string', () => {
8+
const value = 'Mad World'
9+
const countVowels = checkVowels(value)
10+
expect(countVowels).toBe(2)
11+
})
12+
})

0 commit comments

Comments
 (0)