From de65d53a86a630887be9b29755b804e675cc1463 Mon Sep 17 00:00:00 2001 From: Carlos Carvalho Date: Sat, 10 Oct 2020 14:55:46 -0300 Subject: [PATCH] Added new algoritm (#433) --- String/CheckVowels.js | 21 +++++++++++++++++++++ String/CheckVowels.test.js | 12 ++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 String/CheckVowels.js create mode 100644 String/CheckVowels.test.js 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) + }) +})