Avatar of PatrickMcSweeny

PatrickMcSweeny's solution

to Isogram in the JavaScript Track

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

Determine if a word or phrase is an isogram.

An isogram (also known as a "nonpattern word") is a word or phrase without a repeating letter, however spaces and hyphens are allowed to appear multiple times.

Examples of isograms:

  • lumberjacks
  • background
  • downstream
  • six-year-old

The word isograms, however, is not an isogram, because the s repeats.

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

Wikipedia https://en.wikipedia.org/wiki/Isogram

Submitting Incomplete Solutions

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

isogram.spec.js

import { isIsogram } from './isogram';

describe('Isogram Test Suite', () => {
  test('empty string', () => {
    expect(isIsogram('')).toEqual(true);
  });

  xtest('isogram with only lower case characters', () => {
    expect(isIsogram('isogram')).toEqual(true);
  });

  xtest('word with one duplicated character', () => {
    expect(isIsogram('eleven')).toEqual(false);
  });

  xtest('word with one duplicated character from the end of the alphabet', () => {
    expect(isIsogram('zzyzx')).toEqual(false);
  });

  xtest('longest reported english isogram', () => {
    expect(isIsogram('subdermatoglyphic')).toEqual(true);
  });

  xtest('word with duplicated character in mixed case', () => {
    expect(isIsogram('Alphabet')).toEqual(false);
  });

  xtest('word with duplicated character in mixed case, lowercase first', () => {
    expect(isIsogram('alphAbet')).toEqual(false);
  });

  xtest('hypothetical isogrammic word with hyphen', () => {
    expect(isIsogram('thumbscrew-japingly')).toEqual(true);
  });

  xtest('isogram with duplicated hyphen', () => {
    expect(isIsogram('six-year-old')).toEqual(true);
  });

  xtest('made-up name that is an isogram', () => {
    expect(isIsogram('Emily Jung Schwartzkopf')).toEqual(true);
  });

  xtest('duplicated character in the middle', () => {
    expect(isIsogram('accentor')).toEqual(false);
  });

  xtest('same first and last characters', () => {
    expect(isIsogram('angola')).toEqual(false);
  });
});
export const isIsogram = candidate => {
  candidate = candidate.toLowerCase().replace(/\s|-/g, '');
  let uniqueChars = candidate.split("").filter(function(char, index){
    return candidate.indexOf(char) === index;
  });
  return uniqueChars.length === candidate.length;
};

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?