Set with size

Pangram
Pangram in JavaScript
export function isPangram(input) {
  return new Set(input.toLowerCase().match(/[a-z]/g)).size === 26;
}

This approach creates a Set of the unique letters in the input and tests its size to determine the result.

  • It first creates a new Set made from the lowercased characters of the input that only match the regular expression pattern for letters from a-z.
  • The function returns if the size of the Set is 26. If the number of unique letters in the Set is equal to the 26 letters in the alphabet, then the function will return true.

Shortening

When the body of a function is a single expression, the function can be implemented as an arrow function, like so

export const isPangram = (input) =>
  new Set(input.toLowerCase().match(/[a-z]/g)).size === 26;

Notice that return and the curly braces are not needed.

22nd May 2024 · Found it useful?