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
filter
method is then called on theArray
to filter out any character that is nota
-z
. - The letters that survive the filter are assigned to
word
. - A
Set
is initialized from the letters inword
. Thesize
of unique letters is compared with thelength
of 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.
20th Nov 2024
·
Found it useful?