Skip to content

Commit d1d61c4

Browse files
skabbass1matthewmorgan
authored andcommitted
port proverb exercise from javascript track (exercism#340)
1 parent b01cef0 commit d1d61c4

File tree

5 files changed

+240
-0
lines changed

5 files changed

+240
-0
lines changed

config.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,21 @@
665665
"Integers"
666666
]
667667
},
668+
{
669+
"uuid": "5626f5b6-207b-458b-92b6-eff2cadb240a",
670+
"slug": "proverb",
671+
"core": false,
672+
"unlocked_by": "bob",
673+
"difficulty": 4,
674+
"topics": [
675+
"Control flow (conditionals)",
676+
"Control flow (loops)",
677+
"Arrays",
678+
"Strings",
679+
"Text formatting",
680+
"Optional values"
681+
]
682+
},
668683
{
669684
"uuid": "1a6c4a3b-d5db-4a5a-b123-66cf085defe6",
670685
"slug": "flatten-array",

exercises/proverb/README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Proverb
2+
3+
"For want of a horseshoe nail, a kingdom was lost" is a popular proverb. There are may variations
4+
of this proverb and these variations can be chained together. For example:
5+
6+
> For want of a nail the shoe was lost.
7+
> For want of a shoe the horse was lost.
8+
> For want of a horse the rider was lost.
9+
> For want of a rider the message was lost.
10+
> For want of a message the battle was lost.
11+
> For want of a battle the kingdom was lost.
12+
> And all for the want of a horseshoe nail.
13+
14+
Given two or more nouns, construct the full chain of events for the above proverb.
15+
For example, given the nouns "nail", "shoe" and "horse", your program should output the text:
16+
17+
>For want of a nail the shoe was lost.
18+
>For want of a shoe the horse was lost.
19+
>And all for the want of a nail.
20+
21+
Given the nouns, "nail", "shoe", "horse", "rider", "message", and a qualifier "horseshoe", your program should output:
22+
23+
> For want of a nail the shoe was lost.
24+
> For want of a shoe the horse was lost.
25+
> For want of a horse the rider was lost.
26+
> For want of a rider the message was lost.
27+
> And all for the want of a horseshoe nail.
28+
29+
30+
## Setup
31+
32+
Go through the setup instructions for EcmaScript to
33+
install the necessary dependencies:
34+
35+
http://exercism.io/languages/ecmascript
36+
37+
## Requirements
38+
39+
Install assignment dependencies:
40+
41+
```bash
42+
$ npm install
43+
```
44+
45+
## Making the test suite pass
46+
47+
Execute the tests with:
48+
49+
```bash
50+
$ npm test
51+
```
52+
53+
In the test suites all tests but the first have been skipped.
54+
55+
Once you get a test passing, you can enable the next one by
56+
changing `xtest` to `test`.
57+
58+
## Source
59+
60+
Wikipedia [http://en.wikipedia.org/wiki/For_Want_of_a_Nail](http://en.wikipedia.org/wiki/For_Want_of_a_Nail)
61+
62+
## Submitting Incomplete Solutions
63+
It's possible to submit an incomplete solution so you can see how others have completed the exercise.

exercises/proverb/example.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const lastArgIsOptions = (args) => {
2+
const last = args[args.length - 1];
3+
return typeof last === 'object';
4+
};
5+
6+
const conclusion = (firstArg, qualifier = '') => `And all for the want of a ${qualifier}${firstArg}.`;
7+
8+
const proverb = (...args) => {
9+
let options = {};
10+
if (lastArgIsOptions(args)) {
11+
options = args.pop();
12+
}
13+
14+
const allExceptLastArg = args.slice(0, -1);
15+
const chainOfEvents = allExceptLastArg.map((arg, index) => `For want of a ${arg} the ${args[index + 1]} was lost.`);
16+
17+
const qualifier = options.qualifier ? `${options.qualifier} ` : options.qualifier;
18+
chainOfEvents.push(conclusion(args[0], qualifier));
19+
20+
return chainOfEvents.join('\n');
21+
};
22+
23+
export default proverb;

exercises/proverb/package.json

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
{
2+
"name": "xecmascript",
3+
"version": "0.0.0",
4+
"description": "Exercism exercises in ECMAScript 6.",
5+
"author": "Katrina Owen",
6+
"private": true,
7+
"repository": {
8+
"type": "git",
9+
"url": "https://github.com/exercism/xecmascript"
10+
},
11+
"devDependencies": {
12+
"babel-jest": "^20.0.3",
13+
"babel-plugin-transform-builtin-extend": "^1.1.2",
14+
"babel-preset-env": "^1.4.0",
15+
"eslint": "^3.19.0",
16+
"eslint-config-airbnb": "^15.0.1",
17+
"eslint-plugin-import": "^2.2.0",
18+
"eslint-plugin-jsx-a11y": "^5.0.1",
19+
"eslint-plugin-react": "^7.0.1",
20+
"jest": "^20.0.4"
21+
},
22+
"jest": {
23+
"modulePathIgnorePatterns": [
24+
"package.json"
25+
]
26+
},
27+
"babel": {
28+
"presets": [
29+
"env"
30+
],
31+
"plugins": [
32+
[
33+
"babel-plugin-transform-builtin-extend",
34+
{
35+
"globals": [
36+
"Error"
37+
]
38+
}
39+
],
40+
["transform-regenerator"]
41+
]
42+
},
43+
"scripts": {
44+
"test": "jest --no-cache ./*",
45+
"watch": "jest --no-cache --watch ./*",
46+
"lint": "eslint .",
47+
"lint-test": "eslint . && jest --no-cache ./* "
48+
},
49+
"eslintConfig": {
50+
"parserOptions": {
51+
"ecmaVersion": 6,
52+
"sourceType": "module"
53+
},
54+
"env": {
55+
"es6": true,
56+
"node": true,
57+
"jest": true
58+
},
59+
"extends": "airbnb",
60+
"rules": {
61+
"import/no-unresolved": "off",
62+
"import/extensions": "off"
63+
}
64+
},
65+
"licenses": [
66+
"MIT"
67+
],
68+
"dependencies": {}
69+
}

exercises/proverb/proverb.spec.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import proverb from './proverb';
2+
3+
describe('Proverb Test Suite', () => {
4+
test('a single consequence', () => {
5+
const result = proverb('nail', 'shoe');
6+
7+
expect(result).toEqual(
8+
`For want of a nail the shoe was lost.
9+
And all for the want of a nail.`);
10+
});
11+
12+
xtest('a short chain of consequences', () => {
13+
const result = proverb('nail', 'shoe', 'horse');
14+
15+
expect(result).toEqual(
16+
`For want of a nail the shoe was lost.
17+
For want of a shoe the horse was lost.
18+
And all for the want of a nail.`);
19+
});
20+
21+
xtest('a longer chain of consequences', () => {
22+
const result = proverb('nail', 'shoe', 'horse', 'rider');
23+
expect(result).toEqual(
24+
`For want of a nail the shoe was lost.
25+
For want of a shoe the horse was lost.
26+
For want of a horse the rider was lost.
27+
And all for the want of a nail.`);
28+
});
29+
30+
xtest('proverb function does not hard code the rhyme dictionary', () => {
31+
const result = proverb('key', 'value');
32+
33+
expect(result).toEqual(
34+
`For want of a key the value was lost.
35+
And all for the want of a key.`);
36+
});
37+
38+
xtest('the whole proveb', () => {
39+
const result = proverb('nail', 'shoe', 'horse', 'rider',
40+
'message', 'battle', 'kingdom');
41+
42+
expect(result).toEqual(
43+
`For want of a nail the shoe was lost.
44+
For want of a shoe the horse was lost.
45+
For want of a horse the rider was lost.
46+
For want of a rider the message was lost.
47+
For want of a message the battle was lost.
48+
For want of a battle the kingdom was lost.
49+
And all for the want of a nail.`);
50+
});
51+
52+
xtest('proverb is the same each time', () => {
53+
expect(proverb('nail', 'shoe')).toEqual(proverb('nail', 'shoe'));
54+
});
55+
56+
xtest('the use of an optional qualifier in the final consequence', () => {
57+
const result = proverb('nail', 'shoe', 'horse', 'rider',
58+
'message', 'battle', 'kingdom',
59+
{ qualifier: 'horseshoe' });
60+
61+
expect(result).toEqual(
62+
`For want of a nail the shoe was lost.
63+
For want of a shoe the horse was lost.
64+
For want of a horse the rider was lost.
65+
For want of a rider the message was lost.
66+
For want of a message the battle was lost.
67+
For want of a battle the kingdom was lost.
68+
And all for the want of a horseshoe nail.`);
69+
});
70+
});

0 commit comments

Comments
 (0)