删除最后一个元音 - JavaScript
javascriptweb developmentfront end technologyobject oriented programming更新于 2025/12/8 15:40:17
我们需要编写一个 JavaScript 函数,该函数接受一个字符串并返回一个删除了每个单词的最后一个元音的新字符串。
例如 − 如果字符串是 −
const str = 'This is an example string';
那么输出应该是 −
const output = 'Ths s n exampl strng';
示例
以下是代码 −
const str = 'This is an example string';
const removeLast = word => {
const lastIndex = el => word.lastIndexOf(el);
const ind = Math.max(lastIndex('a'), lastIndex('e'), lastIndex('i'),
lastIndex('o'), lastIndex('u'));
return word.substr(0, ind) + word.substr(ind+1, word.length);
}
const removeLastVowel = str => {
const strArr = str.split(' ');
return strArr.reduce((acc, val) => {
return acc.concat(removeLast(val));
}, []).join(' ');
};
console.log(removeLastVowel(str));
输出
以下是控制台中的输出 −
Ths s n exampl strng
相关文章
有用资源
javascript 参考教程 - 该教程包含有关 javascript 的更多信息:https://www.cainiaomax.com/javascript/

