javascript计数元音,辅音和显示出现 [英] javascript counting vowels, consonants and show the occurrance

查看:148
本文介绍了javascript计数元音,辅音和显示出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法弄清楚如何计算元音,辅音并从用户输入中声明元音单独出现的频率。
用户输入任何文本ex:Mischief managed!,结果必须是:
a:2
e:2
i:2
o:0
u: 0
非元音:11

I'm having trouble to figure out how to count vowels, consonants and declare how often the vowels appears separately, from user input. The user put any text ex: "Mischief managed!" and result must be: a: 2 e: 2 i: 2 o: 0 u: 0 non-vowels: 11

var userData = prompt ("Enter any text here");
var a = 0;
var e = 0;
var i = 0;
var o = 0;
var u = 0;
var consonants = 0;
var count;

for (count = 0; count <= userData.legth; count++){
   if((userData.charAt(count).match(/[aeiou]/))){       
     a++;
     e++;
     i++;
     o++;
     u++;
    }else if((userData.charAt(count).match(/[bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ]/))){

    consonants++;
}
}
 console.log ("a: " + a);
 console.log ("e: " + e);
 console.log ("i: " + i);
 console.log ("o: " + o);
 console.log ("u: " + u);
 console.log ("consonants: " + consonants);

但它不起作用。我已经在很多其他论坛上搜索了但是,我没有找到这样的东西。

But it's not working. I already searched in many other forums but, I didn't find anything like this.

推荐答案

首先,让我们指出一些事情

Firstly, let's point out some things

for (count = 0; count <= userData.legth; count++){

长度缺少字母'n',您无需计算小于或等于因为你已经从索引0开始。所以你只需要小于

Length is missing letter 'n' and you don't need count to be less than or equal because you already start from index 0. So you just need less than.

另外:

if((userData.charAt(count).match(/[aeiou]/))){       
    a++;
    e++;
    i++;
    o++;
    u++;
} else if((userData.charAt(count).match(/[bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ]/))){
    consonants++;
}

你在这里做的是每次匹配元音时,你增加所有这些的变量,所以对于单词 hi ,它会打印出每个元音都有一个计数。所以看一下这个:

What you are doing here is that every time it matches a vowel, you increment the variable for all of them, so for the word hi it would print that every vowel had one count. So take a look at this one:

var userData = prompt("Enter any text here").toLowerCase();
var a = 0;
var e = 0;
var i = 0;
var o = 0;
var u = 0;
var consonants = 0;
var count;

for (count = 0; count < userData.length; count++){
    var char = userData.charAt(count);
    if(char.match(/[aeiou]/)){
        switch (char) {
            case 'a':
                a++;
                break;
            case 'e':
                e++;
                break;
            case 'i':
                i++;
                break;
            case 'o':
                o++;
                break;
            case 'u':
                u++;
                break;
        }
    } else if(char.match(/[bcdfghjklmnpqrstvwxyz]/)) {
        consonants++;
    }
}

console.log ("a: " + a);
console.log ("e: " + e);
console.log ("i: " + i);
console.log ("o: " + o);
console.log ("u: " + u);
console.log ("consonants: " + consonants);

我遵循你的逻辑,保持简单,更好的可读性。我们以与您相同的方式匹配正则表达式,但我们也检查现在的确切字符是什么,因此我们可以增加正确的变量。

I am following your logic to keep it simple and better readable for you. We match the regular expression the same way you do, but we also check what is the exact character right now, so we can increment the correct variable.

对于 其他如果 ,为了使正则表达式最小化,我们只检查它是否匹配小写字母,因为我们将userData转换为小写,只要我们得到它:

For the else if, in order to minimize a bit your regular expression we just check if it matches one of the lower case letters, because we convert the userData to lower case, as soon as we get it:

var userData = prompt("Enter any text here").toLowerCase();

试试我的例子,看看它是否符合你的需求。

Try my example and see if it fits your needs.

这篇关于javascript计数元音,辅音和显示出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆