DEV Community

Madalina Pastiu
Madalina Pastiu

Posted on

Detect Pangram

Instructions:

A pangram is a sentence that contains every single letter of the alphabet at least once. For example, the sentence "The quick brown fox jumps over the lazy dog" is a pangram, because it uses the letters A-Z at least once (case is irrelevant).

Given a string, detect whether or not it is a pangram. Return True if it is, False if not. Ignore numbers and punctuation.

Try 1:

function isPangram(string) {
  const alphabet = [
    "a",
    "b",
    "c",
    "d",
    "e",
    "f",
    "g",
    "h",
    "i",
    "j",
    "k",
    "l",
    "m",
    "n",
    "o",
    "p",
    "q",
    "r",
    "s",
    "t",
    "u",
    "v",
    "w",
    "x",
    "y",
    "z",
  ];
  const newArray = [...new Set(string.toLowerCase().split(""))];
  const index1 = newArray.indexOf(" ");
  if (index1 !== -1) newArray.splice(index1, 1);
  const index2 = newArray.indexOf(".");
  if (index2 !== -1) newArray.splice(index2, 1);

  return newArray.length === alphabet.length ? true : false;
}
Enter fullscreen mode Exit fullscreen mode

Thoughts 1:

  1. I do split the string into an array of letters and store it in a set type, so I will only have unique letters.I store everything into the newArray variable.
  2. I identify the indexes of the space(' ') and period('.') punctuation and delete them from the array.
  3. Finally, I do compare the length of the newArray with the alphabet length. Taking into account that each letter in the new array is unique, if the lengths are equal results the string is a pangram. 4.The solution above was my 1st try to solve the challenge and does not work. ### Solution:
function isPangram(string) {
  const alphabet = [
    "a",
    "b",
    "c",
    "d",
    "e",
    "f",
    "g",
    "h",
    "i",
    "j",
    "k",
    "l",
    "m",
    "n",
    "o",
    "p",
    "q",
    "r",
    "s",
    "t",
    "u",
    "v",
    "w",
    "x",
    "y",
    "z",
  ];
   const newArr = [...new Set(string.toLowerCase().split(""))]
    .filter(
      (letter) =>
        letter !== " " &&
        letter !== "." &&
        letter !== "," &&
        isNaN(Number(letter))
    );

  return newArr.length === alphabet.length ? true : false;

}
Enter fullscreen mode Exit fullscreen mode

Thoughts 2:

  1. I have modify my function to include numbers and to eliminate my errors.
  2. I do split the string into an array of letters and store it in a set type, so I will only have unique letters. I store everything into the newArr variable. Then filter the resulted array, making sure they respect all the conditions, no punctuation or numbers.
.filter(
      (letter) =>
        letter !== " " &&
        letter !== "." &&
        letter !== "," &&
        isNaN(Number(letter))
    );
Enter fullscreen mode Exit fullscreen mode
  1. Finally, I do compare the length of the newArray with the alphabet length. Taking into account that each letter in the new array is unique, if the lengths are equal results the string is a pangram.

This is a CodeWars Challenge of 6kyu Rank

Top comments (0)