How to count vowels in a string with JavaScript

2 Answers

0 votes
function vowel_count(str) {
	const vowels = 'aeiouAEIOU';
	let count = 0;
   
	for (let i = 0; i < str.length ; i++)  {
		if (vowels.indexOf(str[i]) !== -1) {
			count += 1;
		}
	}
	
  return count;
}
 
console.log(vowel_count("javascript programming"));
   
   
     
     
/*
run:
 
6
     
*/
 
  

 



answered Sep 18, 2021 by avibootz
0 votes
function vowel_count(str) {
  	const vowels = /[aeiou]/gi;
  	const result = str.match(vowels);
  	const count = result.length;
     
  	return count;
}
  
console.log(vowel_count("javascript programming"));
    
    
      
      
/*
run:
  
6
      
*/

 



answered May 12, 2022 by avibootz

Related questions

1 answer 112 views
1 answer 164 views
1 answer 126 views
1 answer 137 views
1 answer 162 views
162 views asked Jul 26, 2020 by avibootz
1 answer 147 views
...