从JavaScript中的字符串中删除最后一个元音
我们需要编写一个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
热门推荐
10 小红书平安祝福语简短
11 生日祝福语大全女孩简短
12 收生日红包祝福语 简短
13 领证幽默祝福语简短
14 法考面试祝福语简短
15 老哥出门祝福语简短语
16 送灯祝福语简短独特
17 幼儿狗年祝福语大全简短
18 好听的元旦简短祝福语