javascript 查找字符串中元音数量的简单程序不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12921195/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simple program to find number of vowels in a string not working
提问by krtkush
I'm trying my hand on JavaScript and my browser refuses to execute this program I have written using switch case function. The goal is to find the number of vowels in a string (consider all are in-putted in lower case)
我正在尝试使用 JavaScript,但我的浏览器拒绝执行我使用 switch case 函数编写的这个程序。目标是找到字符串中元音的数量(考虑所有输入的都是小写)
When I click the 'submit' button the text from the box goes away but nothing happens. The alert "hello" also does not appear, so I'm assuming that the function is not even being executed.
当我单击“提交”按钮时,框中的文本消失,但没有任何反应。警报“你好”也没有出现,所以我假设该函数甚至没有被执行。
<html>
<head>
<script>
function vow(form)
{
alert("hello");
var a = new Array(10);
a = form.t1.value;
var flag = 0;
var i;
for(i=0;i<10;i++)
{
switch (a[i])
{
case 'a':
flag++;
break;
case 'e':
flag++;
break;
case 'i';
flag++;
break;
case 'o';
flag++;
break;
case 'u';
flag++;
break;
}
}
alert(flag);
}
</script>
</head>
<body>
<form>
<input type="text" name="t1">
<input type="submit" value="SUBMIT" onClick="vow(this.form)"/>
</form>
</body>
</html>
回答by Alexander
You are using ;
instead of :
.
您正在使用;
而不是:
.
case 'i';
__^__
Same case for o
and u
.
o
和 的情况相同u
。
Update:
更新:
I also show a (memory intensive) alternative version:
我还展示了一个(内存密集型)替代版本:
var count = input.match(/[aeiou]/gi).length;
Update:
更新:
var vow = function(str) {
var matches = str.match(/[aeiou]/gi);
var count = matches ? matches.length : 0;
alert("'" + str + "' contains " + count + " vowel(s)");
return false;
}? ?
<input type="submit" value="SUBMIT" onClick="return vow(this.form.t1.value)"/>
回答by Michal Klouda
You can do it much simpler.. E.g. remove the vowels from your input and check the length difference comparing to original string.
你可以做得更简单。例如,从你的输入中删除元音并检查与原始字符串相比的长度差异。
var count = input.length - input.replace(/[aeiou]/gi, '').length;
EDIT: Or even simpler, remove everything but vowels ;-)
编辑:或者更简单,删除除元音之外的所有内容;-)
function vow(form) {
alert(form.t1.value.replace(/[^aeiou]/gi, '').length);
}?
回答by dfsq
A little improved version of your program:
你的程序的一个小改进版本:
function vow(form) {
var a = form.t1.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);
}?
But this task can be handled much simplier:
但是这个任务可以更简单地处理:
function vow(str) {
return --(str.split(/[aeiou]/).length);
}
So: vow("hello!")
-> 2
所以:vow("hello!")
-> 2
回答by Hitham S. AlQadheeb
You can simplify the vow method to
您可以将誓言方法简化为
function vow(form) {
a = form.t1.value;
var matches = a.toString().match(/[aeiou]/g);
if( matches != null){
alert(matches.length);
} else {
alert(0);
}
}
回答by The Alpha
You have used
你用过
case 'i'; case 'o'; case 'u';
should be
应该
case 'i': case 'o': case 'u':
and also use return false at the end of your function, like
并在函数末尾使用 return false ,例如
function vow(form)
{
// code
return false;
}
and also add return
like
并添加return
喜欢
<input type="submit" value="SUBMIT" onClick="return vow(this.form)"/>
DEMO.
演示。
回答by Kent Pawar
[OP] When I click the 'submit' button the text from the box goes away but nothing happens. The alert "hello" also does not appear, so I'm assuming that the function is not even being executed.
[OP] 当我单击“提交”按钮时,框中的文本消失但没有任何反应。警报“你好”也没有出现,所以我假设该函数甚至没有被执行。
You need to user onSubmit event instead of the onClick handler as follows:
您需要使用 onSubmit 事件而不是 onClick 处理程序,如下所示:
<input type="submit" value="SUBMIT" onSubmit ="vow(this.form)"/>