Count the vowels of an input?

后端 未结 6 2069
小鲜肉
小鲜肉 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 11:00

    Replace your switch statement with

    if(vowels.indexof(a[i]) > -1)
    

    So

    function vow(form){
        var vowels = ['a', 'e', 'i', 'o', 'u'];
        var a = form.CountVowels.value;
            flag = 0;
    
        for (var i = 0; i < a.length; i++){
           if(vowels.indexof(a[i]) > -1){
            flag++;
           }
        }
        alert(flag);
    }
    

    If you want to stick with the switch statement, you can, but you were missing colons ':'

                case 'a':
                case 'e':
                case 'i':
                case 'o':
                case 'u':
                    flag++;
                    break;
    

    The first method is simpler as you are basically checking if the letter exists in the vowel array.

提交回复
热议问题