Javascript检查字符是否是元音 [英] Javascript check if character is a vowel
问题描述
我在这里查看了许多与我有关的问题,但是它们都使用与我必须使用的方法不同的方法。我知道这是一种很费时的方法,可以找出何时有更简单的方法,但我只是按照说明进行操作。
为什么下面的代码不起作用?该功能检查其是否是元音。
然后检查输入的长度是否为1。如果为1,则调用该函数。如果大于1,请再输入一次,直到长度为1。
我现在看到JS中不存在布尔值。我猜这个问题现在无效!
函数isVowel(x){
布尔结果;
if(x == A || x == E || x == I || x == O || x == U){
结果= true;
}
else {
result = false;
}
返回结果;
}
var输入;
输入=提示(输入字符);
输入= input.toUpperCase();
if(input.length == 1){
isVowel(input);
}
}
else {
while(input.length!= 1){
提示(输入字符);
if(input.length == 1){
isVowel(input);
}
}
}
alert(isVowel(input));
您正在呼叫 isVowel
放在三个位置,只是将返回值丢在前两个位置。如果要在前两个位置查看返回值,请显示它(通过上一个示例中的 alert
或其他几种方式)。
还有其他问题:
-
为 devqon指出,您已使用
boolean
而不是var
,因此代码不会解析 -
任何时候您发现自己正在写作:
var结果;
if(condition){
result = true;
} else {
结果=假;
}
返回结果;
...停下来并使其:
var结果=条件;
的返回结果;
因此对于
isVowel
:函数isVowel(x){
var结果;
结果= x == A || x == E || x == I || x == O || x == U;
的返回结果;
}
(当然,您可以将它做成单线,但这是
-
在
}
> if 块(合理,一致的格式将使其显而易见) -
您的
输入 >期间
循环永远不会结束,因为您永远不会用提示
-
而不是
if
后跟while
,使用do-while
这是一个仅包含以下内容的更新版本这些更改
function isVowel(x){var result;结果= x == A || x == E || x == I || x == O || x == U;返回结果;}变量输入;执行{输入=提示(输入字符);如果(input.length == 1){alert(isVowel(input)); }} while(input.length!= 1);
I've looked at plenty of questions related to mine here but they're all using different methods to the method I have to use. I'm aware it's a very long winded way to find out when there are simpler ways but I'm just following instructions.
Why doesn't the below code work? The function checks if it's vowel. Then the input is checked to see if it's length is 1. If it's 1, call the function. If it's greater than 1, ask for another input until the length is 1.
I see now that boolean doesn't exist in JS. I guess this question is invalid now!
function isVowel(x){
boolean result;
if(x == "A" || x == "E" || x == "I" || x == "O" || x == "U" ) {
result = true;
}
else{
result = false;
}
return result;
}
var input;
input = prompt("Enter a character ");
input = input.toUpperCase();
if(input.length == 1){
isVowel(input);
}
}
else{
while(input.length != 1){
prompt("Enter a character ");
if(input.length == 1){
isVowel(input);
}
}
}
alert(isVowel(input));
You're calling isVowel
in three places, and just throwing away the return value in the first two. If you want to see the return value in the first two places, show it (via alert
as in your last example, or any of several other ways).
There are other issues as well:
As devqon points out, you've used
boolean
rather thanvar
, so the code won't parseAny time you find yourself writing:
var result; if (condition) { result = true; } else { result = false; } return result;
...stop and make it:
var result = condition; return result;
So for
isVowel
:function isVowel(x) { var result; result = x == "A" || x == "E" || x == "I" || x == "O" || x == "U"; return result; }
(You can, of course, make that a one-liner, but it's easier to debug this way.)
You have an extra
}
after yourif
block (reasonable, consistent formatting would have make that obvious)Your
while
loop will never end, because you never updateinput
with the return value of theprompt
Rather than an
if
followed by awhile
, usedo-while
Here's an updated version with just those changes
function isVowel(x) {
var result;
result = x == "A" || x == "E" || x == "I" || x == "O" || x == "U";
return result;
}
var input;
do {
input = prompt("Enter a character ");
if (input.length == 1) {
alert(isVowel(input));
}
} while (input.length != 1);
这篇关于Javascript检查字符是否是元音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!