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

# LordNahuel's solution

## to Proverb in the JavaScript Track

Published at Oct 02 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.`,
);
});
});
//
// This is only a SKELETON file for the 'Proverb' exercise. It's been provided as a
// convenience to get you started writing code faster.
//

export const proverb = (...list) => {
let result = '';

for (let i = 0; i < list.length - 1; i++) {
result += `For want of a \${list[i]} the \${list[i + 1]} was lost.\n`
}

result += `And all for the want of a \${list[0]}`;

return result;
};