Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> JavaScript

การกำหนดสตริงแปงแกรม (Pangram) ใน JavaScript

สตริงแปงแกรม (Pangram strings)

แปงแกรมคือสตริงที่มีทุกตัวอักษรของอักษรอังกฤษครบ

เราต้องเขียนฟังก์ชัน 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