在 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

更新時間: 11-Dec-2020

639 瀏覽量

開啟您的 職業生涯

完成課程即可獲得認證

開始
廣告
© . All rights reserved.