Avatar of PatrickMcSweeny

PatrickMcSweeny's solution

to Anagram in the JavaScript Track

Published at Aug 20 2019 · 0 comments
Instructions
Test suite
Solution

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".

Setup

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

https://exercism.io/tracks/javascript/installation

Requirements

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.

Source

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.

anagram.spec.js

import { Anagram } from './anagram';

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

    expect(matches).toEqual([]);
  });

  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']);

    expect(matches).toEqual([]);
  });

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

    expect(matches).toEqual(['inlets']);
  });

  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']);

    expect(matches).toEqual([]);
  });

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

    expect(matches).toEqual(['Carthorse']);
  });

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

    expect(matches).toEqual(['carthorse']);
  });

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

    expect(matches).toEqual(['Carthorse']);
  });

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

    expect(matches).toEqual([]);
  });

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

    expect(matches).toEqual([]);
  });

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

    expect(matches).toEqual([]);
  });
});
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("");
    chars.sort();
    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?