javascript “检查一个字母是否是元音” - 函数返回 false?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13905237/
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
"Check if a letter is a vowel" - Function returning false?
提问by ShedInTheGarden
Exercise: Write a function that takes a character (i.e. a string of length 1) and returns true if it is a vowel, false otherwise.
练习:编写一个函数,它接受一个字符(即长度为 1 的字符串),如果它是元音则返回真,否则返回假。
My Code:
我的代码:
var findVowel = function(letter) {
var vowels = ["a", "e", "i", "o", "u"];
for(var i in vowels){
if(letter === i){
return true;
} else {
return false;
}
}
};
findVowel("e");
I've researched high and low and to me the code looks as if it should but it returns false despite if a vowel is given or not.
我已经研究了高和低,对我来说,代码看起来应该是这样,但无论是否给出元音,它都会返回 false。
回答by Adam
Don't use for..in loops with arrays. i
is the index not the value. Also, your code will only check the letter "a". It will never go to the next iteration of the loop because it always returns true or false after the first iteration.
不要对数组使用 for..in 循环。i
是索引而不是值。此外,您的代码只会检查字母“a”。它永远不会进入循环的下一次迭代,因为它在第一次迭代后总是返回 true 或 false。
You need to move return false to be after the loop, so that it will only return false after it has checked against all vowels.
您需要将 return false 移到循环之后,以便它仅在检查所有元音后才会返回 false。
You should also switch to the more "traditional" for..loop style.
您还应该切换到更“传统”的 for..loop 样式。
I won't even get into the whole "is 'y' a vowel?" issue" :)
我什至不会进入整个“'y'是元音吗?” 问题” :)
Here's the fixed up code:
这是固定的代码:
var findVowel = function(letter) {
var vowels = ["a", "e", "i", "o", "u"];
for(var i = 0; i < vowels.length; i++){ // don't use for...in with Arrays
if(letter === vowels[i]){// Use array indexing instead
return true;
}
}
return false;// This is after the loop
};
Try it out on: http://jsfiddle.net/adamzr/3yhFS/
回答by m0sa
i
is the current index in the iterator of vowels
, not the current vowel, hence:
i
是 的迭代器中的当前索引vowels
,而不是当前元音,因此:
if(letter === vowels[i]) ...
回答by Cerbrus
Simply use this, no need to loop through anything:
只需使用它,无需遍历任何内容:
var findVowel = function(letter) {
return "aeiou".indexOf(letter) != -1; // return if the letter is found in "aeiou"
};
Or, my personal favorite:
或者,我个人最喜欢的:
var findVowel = function(letter) {
return ~"aeiou".indexOf(letter);
};
.indexOf()
returns -1
if the parameter isn't found in the string, otherwise, it returns the parameter's position in the string (a int from 0
to string length - 1
).
.indexOf()
返回-1
如果参数未在字符串中发现的,否则,它返回参数的字符串中的位置(从INT0
到string length - 1
)。
So in the first sample, if the .indexOf()
returns -1
, the letter is not a vowel. If it returns any other value, it is. (Hence the != -1
).
所以在第一个示例中,如果.indexOf()
返回-1
,则字母不是元音。如果它返回任何其他值,则为。(因此!= -1
)。
The ~
is a bitwise NOT, inverting that output:-1
becomes 0
--> a false-ish value.X
(Where X is positive or 0) becomes -(X+1)
--> A true-ish value.
这~
是一个按位非,反转输出:-1
变成0
--> 一个假的值。X
(其中 X 为正数或 0)变为-(X+1)
--> 真实值。
This way, the function will return true-ish if the letter is a vowel, and false-ish if it's not.
这样,如果字母是元音,函数将返回 true-ish,如果不是,则返回 false-ish。
If you need a "strict" boolean output, replace the return
with this:
如果您需要“严格”布尔输出,请将其替换为return
:
return !!~"aeiou".indexOf(letter);
The !!
is a double boolean NOT
(So, invert the boolean value twice), this casts the True-ish value to a true
, and a false-ish value (0
) to a false
.
这!!
是一个双布尔值NOT
(因此,将布尔值反转两次),这将 True-ish 值强制转换为 a true
,将 false-ish 值 ( 0
)强制转换为 a false
。
回答by pbhd
You compare only the first element in your vowels to the given letter and return that result. Instead, you have to go thru your whole array, to see if any of your vowels matches.
您只将元音中的第一个元素与给定的字母进行比较并返回该结果。相反,您必须遍历整个数组,以查看是否有任何元音匹配。
回答by Belle
You could also use the .charAt
method with the .toLowerCase
method.
您也可以将.charAt
方法与方法一起使用.toLowerCase
。
function vowelChecker(x) {
// vowelChecker will grab the first letter (character)...
var firstChar = x.toLowerCase().charAt(0);
// Then check if that first letter is a vowel.
if (firstChar === "a" || firstChar === "e" || firstChar === "i" || firstChar === "o" || firstChar === "u") {
// If so... it will log true.
console.log(true);
} else {
// If not... it will log false.
console.log(false);
}
}
回答by Rainer Larin-Fonseca
try
尝试
function checkIfVowel(vowel) {
return /[aeiouAEIOU]/.test(vowel);
}
this function take a character parameter and checks if it is a vowel through regex, finally returns the corresponding value (true or false).
这个函数接受一个字符参数并通过正则表达式检查它是否是元音,最后返回相应的值(真或假)。