在 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
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP