Exercism v3 launches on Sept 1st 2021. Learn more! ๐Ÿš€๐Ÿš€๐Ÿš€
Avatar of PatrickMcSweeny

PatrickMcSweeny's solution

to Anagram in the JavaScript Track

Published at Aug 20 2019 · 0 comments
Test suite


This exercise has changed since this solution was written.

Given a word and a list of possible anagrams, select the correct sublist.

Given "listen" and a list of candidates like "enlists" "google" "inlets" "banana" the program should return a list containing "inlets".


Go through the setup instructions for Javascript to install the necessary dependencies:



Install assignment dependencies:

$ npm install

Making the test suite pass

Execute the tests with:

$ npm test

In the test suites all tests but the first have been skipped.

Once you get a test passing, you can enable the next one by changing xtest to test.


Inspired by the Extreme Startup game https://github.com/rchatley/extreme_startup

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.


import { Anagram } from './anagram';

describe('Anagram', () => {
  test('no matches', () => {
    const subject = new Anagram('diaper');
    const matches = subject.matches(['hello', 'world', 'zombies', 'pants']);


  xtest('detects two anagrams', () => {
    const subject = new Anagram('master');
    const matches = subject.matches(['stream', 'pigeon', 'maters']);

    expect(matches).toEqual(['stream', 'maters']);

  xtest('does not detect anagram subsets', () => {
    const subject = new Anagram('good');
    const matches = subject.matches(['dog', 'goody']);


  xtest('detects anagram', () => {
    const subject = new Anagram('listen');
    const matches = subject.matches(['enlists', 'google', 'inlets', 'banana']);


  xtest('detects three anagrams', () => {
    const subject = new Anagram('allergy');
    const matches = subject.matches(['gallery', 'ballerina', 'regally', 'clergy', 'largely', 'leading']);

    expect(matches).toEqual(['gallery', 'regally', 'largely']);

  xtest('does not detect non-anagrams with identical checksum', () => {
    const subject = new Anagram('mass');
    const matches = subject.matches(['last']);


  xtest('detects anagrams case-insensitively', () => {
    const subject = new Anagram('Orchestra');
    const matches = subject.matches(['cashregister', 'Carthorse', 'radishes']);


  xtest('detects anagrams using case-insensitive subject', () => {
    const subject = new Anagram('Orchestra');
    const matches = subject.matches(['cashregister', 'carthorse', 'radishes']);


  xtest('detects anagrams using case-insensitive possible matches', () => {
    const subject = new Anagram('orchestra');
    const matches = subject.matches(['cashregister', 'Carthorse', 'radishes']);


  xtest('does not detect a anagram if the original word is repeated', () => {
    const subject = new Anagram('go');
    const matches = subject.matches(['go Go GO']);


  xtest('anagrams must use all letters exactly once', () => {
    const subject = new Anagram('tapper');
    const matches = subject.matches(['patter']);


  xtest('words are not anagrams of themselves (case-insensitive)', () => {
    const subject = new Anagram('BANANA');
    const matches = subject.matches(['BANANA', 'Banana', 'banana']);

export class Anagram {
  constructor(subject) {
    this.subject = subject;

  matches(candidates) {
    return candidates.filter(candidate => {
      return (
        candidate.toLowerCase() !== this.subject.toLowerCase() &&
        this.sortChars(candidate) === this.sortChars(this.subject)

  sortChars(string) {
    let chars = string.toLowerCase().split("");
    return chars.join("");

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other peopleโ€™s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?