Added new algoritm (#433)

This commit is contained in:
Carlos Carvalho
2020-10-10 14:55:46 -03:00
committed by GitHub
parent e833b3fd40
commit de65d53a86
2 changed files with 33 additions and 0 deletions

21
String/CheckVowels.js Normal file
View 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 }

View 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)
})
})