Pangram in TypeScript
- One minute read - 125 wordsI’ve been doing Exercism’s TypeScript exercises, and wanted to share my pangram-checking utility.
A pangram is a sentence that contains each of the 26 English letters at least once. Here’s my TypeScript class to determine if a sentence is a pangram.
class Pangram {
input: string;
constructor(input: string) {
this.input = input.toLowerCase();
}
isPangram(): boolean {
const letters = this.input.replace(/[^a-z]/g, '');
return new Set(letters).size === 26;
}
}
export default Pangram;
isPangram/0
does the work: replacing all characters not a-z
with empty
strings (the sentence is already lowercase), breaking the string apart via
new Set
, and comparing the size to 26. 26 is a borderline magical number, but it’s
such a famous one in the English language that I think I get away with it.