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

# Phalkone's solution

## to Proverb in the JavaScript Track

Published at Sep 06 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.`,
);
});
});``````
``````export const proverb = (...input) => {
let output = '';
if(input[input.length-1].qualifier){
var qualifier = input.pop().qualifier;
}
for(let i = 0; i < input.length -1; i++){
output += `For want of a \${input[i]} the \${input[i+1]} was lost.\n`;
}
output += `And all for the want of a\${qualifier ? ' ' + qualifier : ''} \${input[0]}.`;
return output;
};``````

### 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?