🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of Katzen85

Katzen85's solution

to Proverb in the JavaScript Track

Published at Oct 17 2020 · 0 comments
Instructions
Test suite
Solution

For want of a horseshoe nail, a kingdom was lost, or so the saying goes.

Given a list of inputs, generate the relevant proverb. For example, given the list ["nail", "shoe", "horse", "rider", "message", "battle", "kingdom"], you will output the full text of this proverbial rhyme:

For want of a nail the shoe was lost.
For want of a shoe the horse was lost.
For want of a horse the rider was lost.
For want of a rider the message was lost.
For want of a message the battle was lost.
For want of a battle the kingdom was lost.
And all for the want of a nail.

Note that the list of inputs may vary; your solution should be able to handle lists of arbitrary length and content. No line of the output text should be a static, unchanging string; all should vary according to the input given.

Setup

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

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

Requirements

Please cd into exercise directory before running all below commands.

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.

Submitting Solutions

Once you have a solution ready, you can submit it using:

exercism submit proverb.js

Submitting Incomplete Solutions

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

Exercise Source Credits

Wikipedia http://en.wikipedia.org/wiki/For_Want_of_a_Nail

proverb.spec.js

import { proverb } from './proverb';

describe('Proverb Test Suite', () => {
  test('a single consequence', () => {
    const result = proverb('nail', 'shoe');

    expect(result).toEqual(
      `For want of a nail the shoe was lost.
And all for the want of a nail.`,
    );
  });

  xtest('a short chain of consequences', () => {
    const result = proverb('nail', 'shoe', 'horse');

    expect(result).toEqual(
      `For want of a nail the shoe was lost.
For want of a shoe the horse was lost.
And all for the want of a nail.`,
    );
  });

  xtest('a longer chain of consequences', () => {
    const result = proverb('nail', 'shoe', 'horse', 'rider');
    expect(result).toEqual(
      `For want of a nail the shoe was lost.
For want of a shoe the horse was lost.
For want of a horse the rider was lost.
And all for the want of a nail.`,
    );
  });

  xtest('proverb function does not hard code the rhyme dictionary', () => {
    const result = proverb('key', 'value');

    expect(result).toEqual(
      `For want of a key the value was lost.
And all for the want of a key.`,
    );
  });

  xtest('the whole proveb', () => {
    const result = proverb('nail', 'shoe', 'horse', 'rider',
      'message', 'battle', 'kingdom');

    expect(result).toEqual(
      `For want of a nail the shoe was lost.
For want of a shoe the horse was lost.
For want of a horse the rider was lost.
For want of a rider the message was lost.
For want of a message the battle was lost.
For want of a battle the kingdom was lost.
And all for the want of a nail.`,
    );
  });

  xtest('proverb is the same each time', () => {
    expect(proverb('nail', 'shoe')).toEqual(proverb('nail', 'shoe'));
  });

  xtest('the use of an optional qualifier in the final consequence', () => {
    const result = proverb('nail', 'shoe', 'horse', 'rider',
      'message', 'battle', 'kingdom',
      { qualifier: 'horseshoe' });

    expect(result).toEqual(
      `For want of a nail the shoe was lost.
For want of a shoe the horse was lost.
For want of a horse the rider was lost.
For want of a rider the message was lost.
For want of a message the battle was lost.
For want of a battle the kingdom was lost.
And all for the want of a horseshoe nail.`,
    );
  });
});
export const proverb = (...args) => {
  const items = args.filter(item => typeof(item) === 'string');
  const optionalQualifier = args.filter(item => typeof(item) === 'object').map(item => item.qualifier)[0];

  return items.map((item, itmIdx, itmsArray) => (
    itmIdx < itmsArray.length - 1 ?
      `For want of a ${item} the ${itmsArray[itmIdx + 1]} was lost.\n`
      : `And all for the want of a ${optionalQualifier ? `${optionalQualifier} ${itmsArray[0]}` : itmsArray[0]}.`
  )).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?