使用 JavaScript 判斷一個字串是否是包含全部字母的詞


包含全部字母的詞

包含全部字母的詞是一個包含英語字母的所有字母的字串。

我們需要編寫一個 JavaScript 函式,該函式取一個字串作為第一個且唯一引數,並判斷該字串是否是包含全部字母的詞。出於此問題,我們僅考慮小寫字母。

示例

程式碼如下所示 −

 線上演示

const str = 'We promptly judged antique ivory buckles for the next prize';
const isPangram = (str = '') => {
   str = str.toLowerCase();
   const { length } = str;
   const alphabets = 'abcdefghijklmnopqrstuvwxyz';
   const alphaArr = alphabets.split('');
   for(let i = 0; i < length; i++){
      const el = str[i];
      const index = alphaArr.indexOf(el);
      if(index !== -1){
         alphaArr.splice(index, 1);
      };
   };
   return !alphaArr.length;
};
console.log(isPangram(str));

輸出

控制檯中的輸出如下 −

true

更新於: 24-2-2021

3K+ 瀏覽

開啟你的 職業生涯專欄

完成課程以獲得認證

開始學習
廣告
© . All rights reserved.