Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions String/CheckVowels.js
Original file line number Diff line number Diff line change
@@ -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 changes: 12 additions & 0 deletions String/CheckVowels.test.js
Original file line number Diff line number Diff line change
@@ -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)
})
})