Scriptdrill

Level 1: Basics Exercise 12 of 76 Free

Anagram check

A JavaScript exercise from Scriptdrill. Solve it with a code editor in your browser that runs your function against example and hidden tests, and it is checked the moment you submit.

The task

Return `true` when `a` and `b` are anagrams: they use exactly the same letters the same number of times. Ignore upper and lower case and ignore spaces, so `"Dormitory"` and `"dirty room"` are anagrams. Two empty strings are anagrams.

What this exercise teaches

Sorting the characters gives every anagram of a word the same spelling, so one comparison settles it. Comparing only which letters occur is not enough: aab and abb use the same letters with different counts.

A hint if you get stuck

Normalize both strings the same way (lowercase, spaces removed), sort their characters and compare the results. Counting characters in an object or a Map works too.

Try it now, free, no account needed

Every exercise on Scriptdrill is free to try. Open it on the practice page, solve it, and get instant feedback with a hint. The worked solution opens once you solve it or after two tries.

Open this exercise

More exercises in Basics