Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> JavaScript

Xác định chuỗi pangram trong JavaScript

Pangram là chuỗi chứa đủ 26 chữ cái alphabet. Viết hàm kiểm tra chuỗi có phải pangram không (chỉ xét chữ thường).

Ví dụ: 'We promptly judged antique ivory buckles for the next prize' → true.

Ví dụ

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 index = alphaArr.indexOf(str[i]);
    if (index !== -1) alphaArr.splice(index, 1);
  }
  return !alphaArr.length;
};
console.log(isPangram(str));

Kết quả

true