在 JavaScript 中确定全字母串

javascriptweb developmentfront end technology更新于 2024/6/20 22:34:00

全字母串:

全字母串是包含英文字母表的每个字母的字符串。

我们需要编写一个 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

相关文章