所以我正在做一个项目,正如标题所述,我试图找出 javascript 中字符串的第一个字母是否是元音。到目前为止,我的代码看起来像这样。
function startsWithVowel(word){
var vowels = ("aeiouAEIOU");
return word.startswith(vowels);
}
最佳答案
你已经很接近了,只需使用 [0]
对单词进行切片并以这种方式检查:
function startsWithVowel(word){
var vowels = ("aeiouAEIOU");
return vowels.indexOf(word[0]) !== -1;
}
console.log("apple ".concat(startsWithVowel("apple") ? "starts with a vowel" : "does not start with a vowel"));
console.log("banana ".concat(startsWithVowel("banana") ? "starts with a vowel" : "does not start with a vowel"));
关于javascript - 如何在javascript中查找字符串的第一个字母是否为元音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59523864/