javascript - 如何在javascript中查找字符串的第一个字母是否为元音

标签 javascript

所以我正在做一个项目,正如标题所述,我试图找出 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/

相关文章:

javascript - 如果我遵循正常的 D3 方式,我是否将 eventListener 添加到每个 SVG 元素?

javascript - CKEditor 和 Wiki 标记(自定义数据处理器?)

javascript - 您可以在 'mouseup' 上自动关闭 Leaflet EasyButton 吗?

javascript - 完全自动化传递作为开发 pull 请求确认的一部分

javascript - 用 Javascript 数组替换 anchor 代码

javascript - html5 canvas 变换和缓存渐变、曲线和旋转

javascript - Windows 8 应用程序中的 Require.js 代码组织

javascript - PhoneGap 是否支持应用内购买?

javascript - 如何去掉默认的边框半径,Select2

javascript - yyyy-mm-dd 的正则表达式