ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# urgyen's solution

## to Proverb in the JavaScript Track

Published at Sep 25 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

### 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.`,
);
});
});``````
``````const last = xs => xs[xs.length - 1]

export const proverb = (...args) => {
const maybeQualifier = last(args).qualifier
const xs = maybeQualifier ? args.slice(0, -1) : args
const qualifier = maybeQualifier ? `\${maybeQualifier} ` : ""

return xs.map((x, i) => {
if (i === xs.length - 1) {
return `And all for the want of a \${qualifier}\${xs[0]}.`
}

return `For want of a \${x} the \${xs[i + 1]} was lost.`
}).join("\n")
}``````