Counting vowels in a string with JavaScript

I use basic JavaScript to count the number of vowels in a string. Below is the code, but I would like it to clear a bit. Does .includes() help at all, considering it to be a string? I would like to use something like string.includes("a", "e", "i", "o", "u") , if at all it is possible to clear the conditional statement. Also, do I need to convert the input to a string?

 function getVowels(str) { var vowelsCount = 0; //turn the input into a string var string = str.toString(); //loop through the string for (var i = 0; i <= string.length - 1; i++) { //if a vowel, add to vowel count if (string.charAt(i) == "a" || string.charAt(i) == "e" || string.charAt(i) == "i" || string.charAt(i) == "o" || string.charAt(i) == "u") { vowelsCount += 1; } } return vowelsCount; } 
+8
source share
16 answers

You can do this with a little regex:

 function getVowels(str) { var m = str.match(/[aeiou]/gi); return m === null ? 0 : m.length; } 

It just matches the regular expression ( g makes it search the entire string, i makes it case insensitive) and returns the number of matches. We check for null incase that there are no matches (that is, without vowels) and in this case returns 0.

+27
source

Convert the string to an array using the Array.from() method, then use the Array.prototype.filter() method to filter out an array containing only vowels, and then the length property will contain the number of vowels.

 const countVowels = str => Array.from(str) .filter(letter => 'aeiou'.includes(letter)).length; console.log(countVowels('abcdefghijklmnopqrstuvwxyz')); // 5 console.log(countVowels('test')); // 1 console.log(countVowels('ddd')); // 0 
+8
source
 function countVowels(subject) { return subject.match(/[aeiou]/gi).length; } 

You don’t need to convert anything, Javascript error handling is enough to hint you at such a simple function if you need it.

+3
source

Use match , but be careful as it can return null if no match is found

 const countVowels = (subject => (subject.match(/[aeiou]/gi) || []).length); 
+1
source

You can convert a given string to an array using the distribution operator , and then you can filter() characters only for those that are vowels (case insensitive).

After that, you can check the length array to get the total number of vowels in the string:

 const vowel_count = string => [...string].filter(c => 'aeiou'.includes(c.toLowerCase())).length; console.log(vowel_count('aaaa')); // 4 console.log(vowel_count('AAAA')); // 4 console.log(vowel_count('foo BAR baz QUX')); // 5 console.log(vowel_count('Hello, world!')); // 3 
+1
source

Short and ES6, you can use the count (str) function;

 const count = str => (str.match(/[aeiou]/gi) || []).length; 
+1
source

As the introduction of forEach in ES5, this can be achieved by a functional approach, in a more compact way, and also have an account for each vowel and store that are accounted for in the object.

 function vowelCount(str){ let splitString=str.split(''); let obj={}; let vowels="aeiou"; splitString.forEach((letter)=>{ if(vowels.indexOf(letter.toLowerCase())!==-1){ if(letter in obj){ obj[letter]++; }else{ obj[letter]=1; } } }); return obj; } 
+1
source

Just use this function [for ES5]:

 function countVowels(str){ return (str.match(/[aeiou]/gi) == null) ? 0 : str.match(/[aeiou]/gi).length; } 

Will work like a charm

0
source

 count = function(a) { //var a=document.getElementById("t"); console.log(a); //to see input string on console n = a.length; console.log(n); //calculated length of string var c = 0; for (i = 0; i < n; i++) { if ((a[i] == "a") || (a[i] == "e") || (a[i] == "i") || (a[i] == "o") || (a[i] == "u")) { console.log(a[i]); //just to verify c += 1; } } document.getElementById("p").innerText = c; } 
 <p>count of vowels </p> <p id="p"></p> <input id="t" /> <input type="button" value="count" onclick="count(t.value)" /> 
0
source

This can also be solved using . replace () , replacing everything that is not a vowel with an empty string (basically it will remove these characters) and will return the new string length:

 function vowelCount(str) { return str.replace(/[^aeiou]/gi, "").length; }; 

or if you prefer ES6

 const vowelCount = (str) => ( str.replace(/[^aeiou]/gi,"").length ) 
0
source

This is the shortest solution.

  function getCount(str) { return (str.match(/[aeiou]/ig)||[]).length; } 
0
source
 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; } Function vowels(str){ const match = str.match(/[aeiou]/gi); return match ? match.length : 0 ; } 
0
source

using jQuery

 $("button").on("click", function() { var vowels=0; var word=$("#input").val(); console.log(word); for (j=0; j<word.length; j++) { if (word.charAt(j)==="a"|| word.charAt(j)==="e"|| word.charAt(j)==="i"|| word.charAt(j)==="o"|| word.charAt(j)==="u") { vowels++; } } alert(vowels); }); 
0
source

 const containVowels = str => { const helper = ['a', 'e', 'i', 'o', 'u']; const hash = {}; for (let c of str) { if (helper.indexOf(c) !== -1) { if (hash[c]) { hash[c]++; } else { hash[c] = 1; } } } let count = 0; for (let k in hash) { count += hash[k]; } return count; }; console.log(containVowels('aaaa')); 
0
source
  (A) const countVowels = data => [...data.toLowerCase()].filter(char => 'aeiou'.includes(char)).length; (B) const countVowels = data => data.toLowerCase().split('').filter(char => 'aeiou'.includes(char)).length; countVowels("Stackoverflow") // 4 
0
source
 function vowels_count() { var count = 0; var a_count = 0; var e_count = 0; var i_count = 0; var o_count = 0; var u_count = 0; var str_arr = "afhfvhugiusholhriefhkebruguheuhkujhkuthbuhef"; for ( i=0; i <= str_arr.length -1 ; i++) { if ( str_arr[i] == 'a' ) { a_count++; } if ( str_arr[i] == 'e' ) { e_count++; } if ( str_arr[i] == 'i' ) { i_count++; } if ( str_arr[i] == 'o' ) { o_count++; } if ( str_arr[i] == 'u' ) { u_count++; } } console.log("a count is" + " " + a_count ); console.log("e count is" + " " + e_count ); console.log("i count is" + " " + i_count ); console.log("o count is" + " " + o_count ); console.log("u count is" + " "+ u_count ); } 
-1
source

Source: https://habr.com/ru/post/1264607/


All Articles