diff --git a/String/CountVowels.js b/String/CountVowels.js index d9dc42368a..face0a12b9 100644 --- a/String/CountVowels.js +++ b/String/CountVowels.js @@ -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 } diff --git a/String/test/CountVowels.test.js b/String/test/CountVowels.test.js index b05d60e145..e7cd53f6aa 100644 --- a/String/test/CountVowels.test.js +++ b/String/test/CountVowels.test.js @@ -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) }) })