Return Vowels in a string in JavaScript


We are required to write a JavaScript function that takes in a string that might contain some alphabets. The function should count and return the number of vowels that exists in the string.

Example

Following is the code −

const str = 'this is a string';
const countVowels = (str = '') => {
   str = str.toLowerCase();
   const legend = 'aeiou';
   let count = 0;
   for(let i = 0; i < str.length; i++){
      const el = str[i];
      if(!legend.includes(el)){
         continue;
      };
      count++;
   };
   return count;
};
console.log(countVowels(str));

Output

Following is the output on console −

4

Updated on: 11-Dec-2020

563 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements