Skip to content
Open
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
22 changes: 10 additions & 12 deletions String/CountVowels.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
/**
* @function countVowels
* @description Given a string of words or phrases, count the number of vowels.
* @param {String} str - The input string
* @return {Number} - The number of vowels
* @example countVowels("ABCDE") => 2
* @example countVowels("Hello") => 2
* @description Counts the number of vowels (a, e, i, o, u) in a string,
* ignoring case.
* @param {string} str - The input string
* @returns {number} - The number of vowels in the string
* @see https://en.wikipedia.org/wiki/Vowel
* @example countVowels("hello") => 2
* @example countVowels("rhythm") => 0
* @example countVowels("AEIOU") => 5
*/

const countVowels = (str) => {
if (typeof str !== 'string') {
throw new TypeError('Input should be a string')
throw new TypeError('Input must be a string')
}

const vowelRegex = /[aeiou]/gi
const vowelsArray = str.match(vowelRegex) || []

return vowelsArray.length
return (str.match(/[aeiou]/gi) || []).length
}

export { countVowels }
68 changes: 20 additions & 48 deletions String/test/CountVowels.test.js
Original file line number Diff line number Diff line change
@@ -1,67 +1,39 @@
import { countVowels } from '../CountVowels'
import { countVowels } from '../CountVowels.js'

describe('CountVowels', () => {
it('expect throws on use wrong param', () => {
expect(() => countVowels(0)).toThrow()
describe('countVowels', () => {
test("countVowels('hello') returns 2", () => {
expect(countVowels('hello')).toBe(2)
})

it('count the vowels in a string', () => {
const value = 'Mad World'
const count = countVowels(value)
expect(count).toBe(2)
test("countVowels('rhythm') returns 0", () => {
expect(countVowels('rhythm')).toBe(0)
})

it('should return 0 when input is a string with no vowels', () => {
const value = 'bcdfgh'
const count = countVowels(value)
expect(count).toBe(0)
test("countVowels('AEIOU') returns 5", () => {
expect(countVowels('AEIOU')).toBe(5)
})

it('should return 1 when input is a string of length 1 that is a vowel', () => {
const value = 'a'
const count = countVowels(value)
expect(count).toBe(1)
test("countVowels('') returns 0", () => {
expect(countVowels('')).toBe(0)
})

it('should return the correct result when input is in all uppercase letters', () => {
const value = 'ABCDE'
const count = countVowels(value)
expect(count).toBe(2)
test("countVowels('The quick brown fox') returns 5", () => {
expect(countVowels('The quick brown fox')).toBe(5)
})

it('should return the correct result when input is in all lowercase letters', () => {
const value = 'abcdefghi'
const count = countVowels(value)
expect(count).toBe(3)
test('countVowels ignores non-letter characters', () => {
expect(countVowels('a1e2i3o4u5')).toBe(5)
})

it('should return the correct result when input string contains spaces', () => {
const value = 'abc def ghi'
const count = countVowels(value)
expect(count).toBe(3)
test("countVowels('bcdfg') returns 0", () => {
expect(countVowels('bcdfg')).toBe(0)
})

it('should return the correct result when input contains number characters', () => {
const value = 'a1b2c3'
const count = countVowels(value)
expect(count).toBe(1)
test('throws TypeError for non-string input', () => {
expect(() => countVowels(123)).toThrow(TypeError)
})

it('should return the correct result when input contains punctuation characters', () => {
const value = 'a!b.ce)'
const count = countVowels(value)
expect(count).toBe(2)
})

it('should return 0 when the input is an empty string', () => {
const value = ''
const count = countVowels(value)
expect(count).toBe(0)
})

it('should count multiple occurrences of the same vowel in the input', () => {
const value = 'aaaaa'
const count = countVowels(value)
expect(count).toBe(5)
test('throws TypeError for null input', () => {
expect(() => countVowels(null)).toThrow(TypeError)
})
})
Loading