Avatar of w1zeman1p

w1zeman1p's solution

to Anagram in the JavaScript Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code 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".

Setup

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

http://exercism.io/languages/javascript/installation

Running the test suite

The provided test suite uses Jasmine. You can install it by opening a terminal window and running the following command:

npm install -g jasmine

Run the test suite from the exercise directory with:

jasmine anagram.spec.js

In many test suites all but the first test have been marked "pending". Once you get a test passing, activate the next one by changing xit to it.

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

var Anagram = require('./anagram');

describe('Anagram', function () {
  it('no matches', function () {
    var subject = new Anagram('diaper');
    var matches = subject.matches([ 'hello', 'world', 'zombies', 'pants']);

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

  xit('detects simple anagram', function () {
    var subject = new Anagram('ant');
    var matches = subject.matches(['tan', 'stand', 'at']);

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

  xit('does not detect false positives', function () {
    var subject = new Anagram('galea');
    var matches = subject.matches(['eagle']);

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

  xit('detects multiple anagrams', function () {
    var subject = new Anagram('master');
    var matches = subject.matches(['stream', 'pigeon', 'maters']);

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

  xit('does not detect anagram subsets', function () {
    var subject = new Anagram('good');
    var matches = subject.matches(['dog', 'goody']);

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

  xit('detects anagram', function () {
    var subject = new Anagram('listen');
    var matches = subject.matches(['enlists', 'google', 'inlets', 'banana']);

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

  xit('detects multiple anagrams', function () {
    var subject = new Anagram('allergy');
    var matches = subject.matches(['gallery', 'ballerina', 'regally', 'clergy', 'largely', 'leading']);

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

  xit('detects anagrams case-insensitively', function () {
    var subject = new Anagram('Orchestra');
    var matches = subject.matches(['cashregister', 'Carthorse', 'radishes']);

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

  xit('does not detect a word as its own anagram', function () {
    var subject = new Anagram('banana');
    var matches = subject.matches(['Banana']);

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

  xit('matches() accepts string arguments', function () {
    var subject = new Anagram('ant');
    var matches = subject.matches('stand', 'tan', 'at');

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

  xit('matches() accepts single string argument', function () {
    var subject = new Anagram('ant');
    var matches = subject.matches('tan');

    expect(matches).toEqual(['tan']);
  });
});
'use strict';

class Anagram {
  constructor(input) {
    this.main = input;
  }

  matches(candidates) {
    var results = [],
      candidate;

    if (typeof candidates === "string") {
      candidates = [].slice.call(arguments, 0);
    }

    for (candidate of candidates) {
      if (this._match(candidate)) {
        results.push(candidate);
      }
    }

    return results;
  }

  _match(candidate) {
    if (candidate.toLowerCase() === this.main.toLowerCase()) {
      return false;
    }

    function sortedLower(s) {
      return s.toLowerCase().split('').sort().join('');
    }

    return sortedLower(candidate) === sortedLower(this.main);
  }
}

module.exports = Anagram;

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?