使用 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.