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

rootulp's solution

to Anagram in the JavaScript Track

Published at Jul 13 2018 · 0 comments
Test suite


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


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


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.


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.


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

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


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


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


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


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


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


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


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


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

function anagram(subject) {

  var sortedSubject = sortWord(subject)

  function findMatches(words) {
    if (typeof words === 'string') {
      words = Array.prototype.slice.call(arguments)
    return words.filter(isAnagram)

  function isAnagram(word) {
    return (differentWord(subject, word) && sameSorted(subject, word))

  function differentWord(subject, word) {
    return subject.toLowerCase() !== word.toLowerCase()

  function sameSorted(subject, word) {
    var sortedWord = sortWord(word)
    return sortedSubject === sortedWord

  function sortWord(word) {
    return word.toLowerCase().split('').sort().join('')

  return {
    matches: findMatches

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?