mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-07 02:05:08 +08:00
Added new algoritm (#433)
This commit is contained in:
21
String/CheckVowels.js
Normal file
21
String/CheckVowels.js
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
/*
|
||||||
|
Given a string of words or phrases, count the number of vowels.
|
||||||
|
Example: input = "hello world" return 3
|
||||||
|
*/
|
||||||
|
|
||||||
|
const checkVowels = (value) => {
|
||||||
|
if (typeof value !== 'string') {
|
||||||
|
throw new TypeError('The first param should be a string')
|
||||||
|
}
|
||||||
|
const vowels = ['a', 'e', 'i', 'o', 'u']
|
||||||
|
let countVowels = 0
|
||||||
|
for (let i = 0; i < value.length; i++) {
|
||||||
|
const char = value[i].toLowerCase()
|
||||||
|
if (vowels.includes(char)) {
|
||||||
|
countVowels++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return countVowels
|
||||||
|
}
|
||||||
|
|
||||||
|
export { checkVowels }
|
12
String/CheckVowels.test.js
Normal file
12
String/CheckVowels.test.js
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import { checkVowels } from './CheckVowels'
|
||||||
|
|
||||||
|
describe('Test the checkVowels function', () => {
|
||||||
|
it('expect throws on use wrong param', () => {
|
||||||
|
expect(() => checkVowels(0)).toThrow()
|
||||||
|
})
|
||||||
|
it('count the vowels in a string', () => {
|
||||||
|
const value = 'Mad World'
|
||||||
|
const countVowels = checkVowels(value)
|
||||||
|
expect(countVowels).toBe(2)
|
||||||
|
})
|
||||||
|
})
|
Reference in New Issue
Block a user