Exercism v3 launches on Sept 1st 2021. Learn more! 🚀🚀🚀
Avatar of rootulp

rootulp's solution

to Pangram 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.

Determine if a sentence is a pangram. A pangram (Greek: παν γράμμα, pan gramma, "every letter") is a sentence using every letter of the alphabet at least once. The best known English pangram is:

The quick brown fox jumps over the lazy dog.

The alphabet used consists of ASCII letters a to z, inclusive, and is case insensitive. Input will not contain non-ASCII symbols.

Setup

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

http://exercism.io/languages/ecmascript

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/Pangram

Submitting Incomplete Solutions

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

pangram.spec.js

import Pangram from './pangram';

describe('Pangram()', () => {
  test('empty sentence', () => {
    const pangram = new Pangram('');
    expect(pangram.isPangram()).toBe(false);
  });

  xtest('pangram with only lower case', () => {
    const pangram = new Pangram('the quick brown fox jumps over the lazy dog');
    expect(pangram.isPangram()).toBe(true);
  });

  xtest("missing character 'x'", () => {
    const pangram = new Pangram('a quick movement of the enemy will jeopardize five gunboats');
    expect(pangram.isPangram()).toBe(false);
  });

  xtest("another missing character 'x'", () => {
    const pangram = new Pangram('the quick brown fish jumps over the lazy dog');
    expect(pangram.isPangram()).toBe(false);
  });

  xtest('pangram with underscores', () => {
    const pangram = new Pangram('the_quick_brown_fox_jumps_over_the_lazy_dog');
    expect(pangram.isPangram()).toBe(true);
  });

  xtest('pangram with numbers', () => {
    const pangram = new Pangram('the 1 quick brown fox jumps over the 2 lazy dogs');
    expect(pangram.isPangram()).toBe(true);
  });

  xtest('missing letters replaced by numbers', () => {
    const pangram = new Pangram('7h3 qu1ck brown fox jumps ov3r 7h3 lazy dog');
    expect(pangram.isPangram()).toBe(false);
  });

  xtest('pangram with mixed case and punctuation', () => {
    const pangram = new Pangram('"Five quacking Zephyrs jolt my wax bed."');
    expect(pangram.isPangram()).toBe(true);
  });

  xtest('pangram with non-ascii characters', () => {
    const pangram = new Pangram('Victor jagt zwölf Boxkämpfer quer über den großen Sylter Deich.');
    expect(pangram.isPangram()).toBe(true);
  });
});
const ALPHABET = 'abcdefghijklmnopqrstuvwxyz'.split('');

class Pangram {
  constructor(sentence) {
    this.sentence = sentence.toLowerCase();
  }

  isPangram() {
    if (this.emptySentence()) {
      return false;
    }
    return this.alphabetInSentence();
  }

  emptySentence() {
    return this.sentence === '';
  }

  alphabetInSentence() {
    const alphabet = new Set(ALPHABET);
    [...this.sentence].forEach(char => alphabet.delete(char));
    return alphabet.size === 0;
  }
}

export default Pangram;

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?