EXERCISE · AlgoCasts
Vowels
Write a function that returns the number of vowels used in a string. Vowels are the characters 'a', 'e' 'i', 'o', and 'u'.
Directions
Directions
Write a function that returns the number of vowels used in a string. Vowels are the characters 'a', 'e' 'i', 'o', and 'u'.
Examples#
JS
vowels('Hi There!') --> 3vowels('Why do you ask?') --> 4vowels('Why?') --> 0Try it first#
Copy the stub, make the 5 tests pass on your machine, then open the solution. The reveal is remembered per page in this browser.
JSexercises/vowels/index.js
function vowels(str) {} module.exports = vowels;Solution#
JScompleted_exercises/vowels/index.js
function vowels(str) { const matches = str.match(/[aeiou]/gi); return matches ? matches.length : 0;} module.exports = vowels;function vowels(str) { let count = 0; const checker = ['a', 'e', 'i', 'o', 'u']; for (let char of str.toLowerCase()) { if (checker.includes(char)) { count++; } } return count;}const vowels = require('./index'); test('Vowels is a function', () => { expect(typeof vowels).toEqual('function');}); test('returns the number of vowels used', () => { expect(vowels('aeiou')).toEqual(5);}); test('returns the number of vowels used when they are capitalized', () => { expect(vowels('AEIOU')).toEqual(5);}); test('returns the number of vowels used', () => { expect(vowels('abcdefghijklmnopqrstuvwxyz')).toEqual(5);}); test('returns the number of vowels used', () => { expect(vowels('bcdfghjkl')).toEqual(0);}); Verbatim from the repo at 30cf7634. Alternate tabs are the commented-out solutions with the leading // removed and nothing else changed. Run the tests locally with npx jest vowels inside the fork.
See also