Easy

Count the Vowels

Description

In this challenge, you are required to write a function that counts the number of vowels ("a, e, i, o, u") in a given sentence.\

Vowels are an essential part of text analysis and are used in many language processing applications, such as readability analysis and phonetic matchin\

To successfully complete the task, your function should handle the following considerations:

Handling Punctuation

The sentence may contain punctuation marks such as periods (.), commas (,), exclamation marks (!), question marks (?), colons (:), and semicolons (;).
Your solution should ignore these punctuation marks and consider only the vowels.

Case Sensitivity

The function should count vowels regardless of their case. Both uppercase and lowercase vowels should be considered the same.

Example Usage

countVowels("Hello World"); // ➞ 3
countVowels("I love JavaScript!"); // ➞ 7
countVowels("xyz"); // ➞ 0
countVowels("AEIOU aeiou"); // ➞ 10

Constraints

  • The input will always be a string.
  • The string may contain spaces, punctuation, and numbers.
  • The function should return an integer.

Hint

Consider using the

.toLowerCase()
method to simplify the comparison, and check for vowels using a loop or a regular expression.

Function Signature:

/**
* @param {string} sentence - The sentence to count vowels in.\
* @returns {number} - The number of vowels in the sentence.\
*/
function countVowels(sentence) {
// Your code goes here\
}

window code 2Test Cases

Input:

"Hello World"

Expected Output:

3