diff --git a/String/CheckVowels.js b/String/CheckVowels.js new file mode 100644 index 000000000..d362d82bc --- /dev/null +++ b/String/CheckVowels.js @@ -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 } diff --git a/String/CheckVowels.test.js b/String/CheckVowels.test.js new file mode 100644 index 000000000..fd074e924 --- /dev/null +++ b/String/CheckVowels.test.js @@ -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) + }) +})