移除最后一个元音 - 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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP