javascript print vowels from string code example

Example 1: count vowels in javascript

const vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'];

function countVowels(sentence) {
  let counts = 0;
  for(let i = 0; i < vowels.length; i++) {
    if(vowels.includes(sentence[i])) {
      counts++;
    }
  }
  return console.log(counts);
}

countVowels('Hello World');
countVowels('AaEeIiOoUu');
countVowels('aaaaa');

Example 2: find vowel & consonants in a string java script

const str = "The quick brown fox jumps over a lazy dog"; 
const vowels = str.match(/[aeiou]/gi); 
const consonants = str.match(/[^aeiou]/gi);   
vowels.concat([''],consonants).forEach(k => { console.log(k); } );

Example 3: vowels consonants javascript

//Ask to the user to type a letter
function readLetter() {
    return prompt("Type a letter: ");
}

//Defines true if is a vowel(uppercase or lowercase);
function isVowel(character) {
    switch (character) {
        case "a":
        case "A":
        case "e":
        case "E":
        case "i":
        case "I":  
        case "o":
        case "O":
        case "u":
        case "U": 
            return true;
        default:
            return false;
    }
}

// Brings a dependent message if previous result is true or false.
var letter = readLetter();
if (isVowel(letter)) alert("The letter is a vowel.");
else alert("The letter is consonant.");

Example 4: count vowels in a string javascript

// BEST and FASTER implementation using regex
const countVowels = (str) => (str.match(/[aeiou]/gi) || []).length

Example 5: find vowels in string javascript

var name = userInput[0];
    var count =0;
  	function findvowel(name)
    {
    	var vowel = ['a','e','i','o','u','A','E','I','O','U'];
      	for(i=0;i<name.length;i++)
      	{
      		if(vowel.includes(name[i]))
        	{
          		count++;
        	}
      	}
      	return count;
    }
    
  console.log(findvowel(name));