Skip to content

Commit f468192

Browse files
author
programmiri
committed
Add excercise words and first solution
1 parent 1f01e08 commit f468192

File tree

7 files changed

+5077
-0
lines changed

7 files changed

+5077
-0
lines changed

word-count/.eslintrc

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"root": true,
3+
"parser": "babel-eslint",
4+
"parserOptions": {
5+
"ecmaVersion": 7,
6+
"sourceType": "module"
7+
},
8+
"env": {
9+
"es6": true,
10+
"node": true,
11+
"jest": true
12+
},
13+
"extends": [
14+
"eslint:recommended",
15+
"plugin:import/errors",
16+
"plugin:import/warnings"
17+
],
18+
"rules": {
19+
"linebreak-style": "off",
20+
21+
"import/extensions": "off",
22+
"import/no-default-export": "off",
23+
"import/no-unresolved": "off",
24+
"import/prefer-default-export": "off"
25+
}
26+
}

word-count/README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Word Count
2+
3+
Given a phrase, count the occurrences of each word in that phrase.
4+
5+
For example for the input `"olly olly in come free"`
6+
7+
```text
8+
olly: 2
9+
in: 1
10+
come: 1
11+
free: 1
12+
```
13+
14+
## Setup
15+
16+
Go through the setup instructions for Javascript to install the necessary
17+
dependencies:
18+
19+
[https://exercism.io/tracks/javascript/installation](https://exercism.io/tracks/javascript/installation)
20+
21+
## Requirements
22+
23+
Install assignment dependencies:
24+
25+
```bash
26+
$ npm install
27+
```
28+
29+
## Making the test suite pass
30+
31+
Execute the tests with:
32+
33+
```bash
34+
$ npm test
35+
```
36+
37+
In the test suites all tests but the first have been skipped.
38+
39+
Once you get a test passing, you can enable the next one by changing `xtest` to
40+
`test`.
41+
42+
## Source
43+
44+
This is a classic toy problem, but we were reminded of it by seeing it in the Go Tour.
45+
46+
## Submitting Incomplete Solutions
47+
48+
It's possible to submit an incomplete solution so you can see how others have
49+
completed the exercise.

word-count/babel.config.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module.exports = {
2+
presets: [
3+
[
4+
'@babel/env',
5+
{
6+
targets: {
7+
node: 'current',
8+
},
9+
useBuiltIns: false,
10+
},
11+
12+
],
13+
],
14+
};

word-count/package.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "exercism-javascript",
3+
"description": "Exercism exercises in Javascript.",
4+
"author": "Katrina Owen",
5+
"private": true,
6+
"repository": {
7+
"type": "git",
8+
"url": "https://github.com/exercism/javascript"
9+
},
10+
"devDependencies": {
11+
"@babel/cli": "^7.5.5",
12+
"@babel/core": "^7.5.5",
13+
"@babel/preset-env": "^7.5.5",
14+
"@types/jest": "^24.0.16",
15+
"@types/node": "^12.6.8",
16+
"babel-eslint": "^10.0.2",
17+
"babel-jest": "^24.8.0",
18+
"eslint": "^6.1.0",
19+
"eslint-plugin-import": "^2.18.2",
20+
"jest": "^24.8.0"
21+
},
22+
"jest": {
23+
"modulePathIgnorePatterns": [
24+
"package.json"
25+
]
26+
},
27+
"scripts": {
28+
"test": "jest --no-cache ./*",
29+
"watch": "jest --no-cache --watch ./*",
30+
"lint": "eslint .",
31+
"lint-test": "eslint . && jest --no-cache ./* "
32+
},
33+
"license": "MIT",
34+
"dependencies": {}
35+
}

word-count/word-count.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export class Words {
2+
count(string) {
3+
const words = string.toLowerCase().trim().split(/[\s]+/g)
4+
5+
return words.reduce((acc, curr) => {
6+
if (acc[curr] && typeof acc[curr] === 'number' ) {
7+
acc[curr] = acc[curr] + 1
8+
} else {
9+
acc[curr] = 1
10+
}
11+
return acc
12+
}, {})
13+
}
14+
}

word-count/word-count.spec.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import { Words } from './word-count';
2+
3+
describe('words()', () => {
4+
const words = new Words();
5+
6+
test('counts one word', () => {
7+
const expectedCounts = { word: 1 };
8+
expect(words.count('word')).toEqual(expectedCounts);
9+
});
10+
11+
test('counts one of each', () => {
12+
const expectedCounts = { one: 1, of: 1, each: 1 };
13+
expect(words.count('one of each')).toEqual(expectedCounts);
14+
});
15+
16+
test('counts multiple occurrences', () => {
17+
const expectedCounts = {
18+
one: 1, fish: 4, two: 1, red: 1, blue: 1,
19+
};
20+
expect(words.count('one fish two fish red fish blue fish')).toEqual(expectedCounts);
21+
});
22+
23+
test('includes punctuation', () => {
24+
const expectedCounts = {
25+
car: 1, ':': 2, carpet: 1, as: 1, java: 1, 'javascript!!&@$%^&': 1,
26+
};
27+
expect(words.count('car : carpet as java : javascript!!&@$%^&')).toEqual(expectedCounts);
28+
});
29+
30+
test('includes numbers', () => {
31+
const expectedCounts = { testing: 2, 1: 1, 2: 1 };
32+
expect(words.count('testing 1 2 testing')).toEqual(expectedCounts);
33+
});
34+
35+
test('normalizes to lower case', () => {
36+
const expectedCounts = { go: 3 };
37+
expect(words.count('go Go GO')).toEqual(expectedCounts);
38+
});
39+
40+
test('counts properly international characters', () => {
41+
const expectedCounts = {
42+
'¡hola!': 1, '¿qué': 1, 'tal?': 1, 'привет!': 1,
43+
};
44+
expect(words.count('¡Hola! ¿Qué tal? Привет!')).toEqual(expectedCounts);
45+
});
46+
47+
test('counts multiline', () => {
48+
const expectedCounts = { hello: 1, world: 1 };
49+
expect(words.count('hello\nworld')).toEqual(expectedCounts);
50+
});
51+
52+
test('counts tabs', () => {
53+
const expectedCounts = { hello: 1, world: 1 };
54+
expect(words.count('hello\tworld')).toEqual(expectedCounts);
55+
});
56+
57+
test('counts multiple spaces as one', () => {
58+
const expectedCounts = { hello: 1, world: 1 };
59+
expect(words.count('hello world')).toEqual(expectedCounts);
60+
});
61+
62+
test('does not count leading or trailing whitespace', () => {
63+
const expectedCounts = { introductory: 1, course: 1 };
64+
expect(words.count('\t\tIntroductory Course ')).toEqual(expectedCounts);
65+
});
66+
67+
test('handles properties that exist on Object’s prototype', () => {
68+
const expectedCounts = {
69+
reserved: 1, words: 1, like: 1, constructor: 1, and: 1, tostring: 1, 'ok?': 1,
70+
};
71+
expect(words.count('reserved words like constructor and toString ok?')).toEqual(expectedCounts);
72+
});
73+
});

0 commit comments

Comments
 (0)