Counting number of vowels in a string with JavaScript

前端 未结 18 1600
旧巷少年郎
旧巷少年郎 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;
    }

    count of vowels

提交回复
热议问题