Counting number of vowels in a string with JavaScript

前端 未结 18 1496
旧巷少年郎
旧巷少年郎 2020-12-08 23:53

I\'m using basic JavaScript to count the number of vowels in a string. The below code works but I would like to have it cleaned up a bit. Would using .includes()

相关标签:
18条回答
  • 2020-12-09 00:36

    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 讨论(0)
  • 2020-12-09 00:39

    This could also be solved using .replace() method by replacing anything that isn't a vowel with an empty string (basically it will delete those characters) and returning 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 讨论(0)
  • 2020-12-09 00:41

    One more method (using reduce):

       function getVowels(str) {
         return Array.from(str).reduce((count, letter) => count + 'aeiou'.includes(letter), 0);
       }
    
    0 讨论(0)
  • 2020-12-09 00:42

    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 讨论(0)
  • 2020-12-09 00:43

    You can convert the given string into an array using the spread operator, and then you can filter() the characters to only those which are vowels (case-insensitive).

    Afterwards, you can check the length of the array to obtain 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

    0 讨论(0)
  • 2020-12-09 00:43

    Use this function to get the count of vowels within a string. Works pretty well.

    function getVowelsCount(str)
    {
      //splits the vowels string into an array => ['a','e','i','o','u','A'...]
      let arr_vowel_list = 'aeiouAEIOU'.split(''); 
    
    
      let count = 0;
      /*for each of the elements of the splitted string(i.e. str), the vowels list would check 
        for any occurence and increments the count, if present*/
      str.split('').forEach(function(e){
      if(arr_vowel_list.indexOf(e) !== -1){
       count++;} });
    
    
       //and now log this count
       console.log(count);}
    
    
    //Function Call
    getVowelsCount("World Of Programming");
    

    Output for the given string would be 5. Try this out.

    //Code -

      function getVowelsCount(str)
       {
         let arr_vowel_list = 'aeiouAEIOU'.split(''); 
         let count = 0;
         str.split('').forEach(function(e){
         if(arr_vowel_list.indexOf(e) !== -1){
         count++;} });
         console.log(count);
       }
    
    0 讨论(0)
提交回复
热议问题