gpt4 book ai didi

javascript - 我正在编写一个代码来去掉单词中的最后一个元音,但它正在删除最后一个字母。我哪里错了?

转载 作者:行者123 更新时间:2023-11-29 19:11:37 25 4
gpt4 key购买 nike

我正在编写一个代码来去掉单词中的最后一个元音,但它删除了最后一个字母。我哪里错了?

 function removeLastVowel(word) {
var vowels = ["a", 'e', 'i', 'o', 'u'];
for (var i = word.length - 1; i >= 0; i--) {
if (vowels.indexOf(word[i]) !== undefined) {
return (word.slice(0, i) + word.slice(i + 1));
}
}
return word;
}

console.log(removeLastVowel("heard"));

最佳答案

indexOf() 如果没有找到返回-1,不是undefined

关于javascript - 我正在编写一个代码来去掉单词中的最后一个元音,但它正在删除最后一个字母。我哪里错了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38467016/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com