export function isIsogram(string) {
let word = [...string.toLowerCase()].filter(
(letter) => letter >= 'a' && letter <= 'z',
);
return new Set(word).size == word.length;
}
With this approach you will instantiate a Set of the used letters and compare its size with the filtered word length.
- First, the lowercased input string is made into an Array of its characters using spread syntax.
- The
filtermethod is then called on theArrayto filter out any character that is nota-z. - The letters that survive the filter are assigned to
word. - A
Setis initialized from the letters inword. Thesizeof unique letters is compared with thelengthof the filtered wordlength.
If the number of unique letters in the Set of used letters is the same as the number of filtered letters,
then the function returns true.
4th Sep 2024
·
Found it useful?