The challenge
Write isAnagram(a, b) that returns true if both texts use exactly the same letters, ignoring case and spaces.
isAnagram("listen", "silent") // true
isAnagram("Dormitory", "dirty room") // true
Try it
Write your answer below, then press Check my answer. Your code runs right here in your browser, and the tests tell you what works.
It prints
What the tests check
isAnagram("listen", "silent")givestrueisAnagram("Dormitory", "dirty room")givestrueisAnagram("hello", "world")givesfalseisAnagram("aab", "abb")givesfalse
Hints
Stuck? Open one hint at a time. Try again after each one.
Hint 1
Make a helper that lowercases, removes spaces, splits into letters, sorts and joins.
Hint 2
Two texts are anagrams if their sorted letters are identical.
A solution
There’s always more than one right way. Read it, make sure you understand it, then try writing it yourself without looking.
Show one possible solution
function isAnagram(a, b) {
const key = (s) => s.toLowerCase().replaceAll(" ", "").split("").sort().join("");
return key(a) === key(b);
}
Learn what you need
This exercise uses what Grade 10: Array Power Tools: map, filter & reduce teaches. Read those notes first if any of it is new.