在 JavaScript 中返回字串中的母音
我們要求編寫一個 JavaScript 函式,該函式採用可能包含某些字母的字串。該函式應計算並返回字串中存在的母音數。
例項
以下是程式碼 −
const str = 'this is a string';
const countVowels = (str = '') => {
str = str.toLowerCase();
const legend = 'aeiou';
let count = 0;
for(let i = 0; i < str.length; i++){
const el = str[i];
if(!legend.includes(el)){
continue;
};
count++;
};
return count;
};
console.log(countVowels(str));輸出
以下是控制檯上的輸出 −
4
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP