Count the vowels of an input?

后端 未结 6 2019
小鲜肉
小鲜肉 2021-01-27 10:25

Hi I am trying to count the vowels of the input of the form in my HTML using javascript currently with no success.

Here is my HTML


         


        
6条回答
  •  青春惊慌失措
    2021-01-27 10:57

    The vow function expects the form as a parameter, but you're not passing it. Give your form an ID so you can look it up and pass it.

    button1.onclick = function() {
        vow(document.getElementById('demo_form'));
    };
    

    function vow(form) {
      var a = form.CountVowels.value;
      flag = 0;
    
      for (var i = 0; i < a.length; i++) {
        switch (a[i]) {
          case 'a':
          case 'e':
          case 'i':
          case 'o':
          case 'u':
          flag++;
          break;
        }
      }
      alert(flag);
    }
    
    function init() {
        var button1 = document.getElementById("btn1")
        button1.onclick = function() {
          vow(document.getElementById('demo_form'));
        };
    }
    
    window.onload = init;
    Phrase:

    You were also missing the : character after each case statement.

提交回复
热议问题