Skip to content

Commit 9ae3566

Browse files
committed
Add triangle
1 parent bf71261 commit 9ae3566

File tree

7 files changed

+5091
-0
lines changed

7 files changed

+5091
-0
lines changed

Diff for: triangle/.eslintrc

+26
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+
}

Diff for: triangle/README.md

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Triangle
2+
3+
Determine if a triangle is equilateral, isosceles, or scalene.
4+
5+
An _equilateral_ triangle has all three sides the same length.
6+
7+
An _isosceles_ triangle has at least two sides the same length. (It is sometimes
8+
specified as having exactly two sides the same length, but for the purposes of
9+
this exercise we'll say at least two.)
10+
11+
A _scalene_ triangle has all sides of different lengths.
12+
13+
## Note
14+
15+
For a shape to be a triangle at all, all sides have to be of length > 0, and
16+
the sum of the lengths of any two sides must be greater than or equal to the
17+
length of the third side. See [Triangle Inequality](https://en.wikipedia.org/wiki/Triangle_inequality).
18+
19+
## Dig Deeper
20+
21+
The case where the sum of the lengths of two sides _equals_ that of the
22+
third is known as a _degenerate_ triangle - it has zero area and looks like
23+
a single line. Feel free to add your own code/tests to check for degenerate triangles.
24+
25+
## Requirements
26+
27+
Install assignment dependencies:
28+
29+
```bash
30+
$ yarn
31+
```
32+
33+
## Making the test suite pass
34+
35+
Execute the tests with:
36+
37+
```bash
38+
$ yarn test
39+
```
40+
41+
## Source
42+
43+
The Ruby Koans triangle project, parts 1 & 2 [http://rubykoans.com](http://rubykoans.com)

Diff for: triangle/babel.config.js

+14
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+
};

Diff for: triangle/package.json

+35
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+
}

Diff for: triangle/triangle.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
function isValidTriangle(s1, s2, s3) {
2+
const sidesSorted = [s1, s2, s3].sort((a, b) => a - b);
3+
4+
return (
5+
sidesSorted[0] > 0 && sidesSorted[0] + sidesSorted[1] >= sidesSorted[2]
6+
);
7+
}
8+
9+
function classifyTriangle(s1, s2, s3) {
10+
const sidesSorted = [s1, s2, s3].sort((a, b) => a - b);
11+
12+
if (sidesSorted[0] === sidesSorted[2]) {
13+
return "equilateral";
14+
} else if (
15+
sidesSorted[0] === sidesSorted[1] ||
16+
sidesSorted[1] === sidesSorted[2]
17+
) {
18+
return "isosceles";
19+
} else {
20+
return "scalene";
21+
}
22+
}
23+
24+
export class Triangle {
25+
constructor(s1, s2, s3) {
26+
this.s1 = s1;
27+
this.s2 = s2;
28+
this.s3 = s3;
29+
}
30+
31+
kind() {
32+
if (!isValidTriangle(this.s1, this.s2, this.s3)) {
33+
throw "invalid triangle";
34+
}
35+
36+
return classifyTriangle(this.s1, this.s2, this.s3);
37+
}
38+
}

Diff for: triangle/triangle.spec.js

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { Triangle } from './triangle';
2+
3+
describe('Triangle', () => {
4+
test('equilateral triangles have equal sides', () => {
5+
const triangle = new Triangle(2, 2, 2);
6+
expect(triangle.kind()).toEqual('equilateral');
7+
});
8+
9+
test('larger equilateral triangles also have equal sides', () => {
10+
const triangle = new Triangle(10, 10, 10);
11+
expect(triangle.kind()).toEqual('equilateral');
12+
});
13+
14+
test('isosceles triangles have last two sides equal', () => {
15+
const triangle = new Triangle(3, 4, 4);
16+
expect(triangle.kind()).toEqual('isosceles');
17+
});
18+
19+
test('isosceles trianges have first and last sides equal', () => {
20+
const triangle = new Triangle(4, 3, 4);
21+
expect(triangle.kind()).toEqual('isosceles');
22+
});
23+
24+
test('isosceles triangles have two first sides equal', () => {
25+
const triangle = new Triangle(4, 4, 3);
26+
expect(triangle.kind()).toEqual('isosceles');
27+
});
28+
29+
test('isosceles triangles have in fact exactly two sides equal', () => {
30+
const triangle = new Triangle(10, 10, 2);
31+
expect(triangle.kind()).toEqual('isosceles');
32+
});
33+
34+
test('scalene triangles have no equal sides', () => {
35+
const triangle = new Triangle(3, 4, 5);
36+
expect(triangle.kind()).toEqual('scalene');
37+
});
38+
39+
test('scalene triangles have no equal sides at a larger scale too', () => {
40+
const triangle = new Triangle(10, 11, 12);
41+
expect(triangle.kind()).toEqual('scalene');
42+
});
43+
44+
test('scalene triangles have no equal sides in descending order either', () => {
45+
const triangle = new Triangle(5, 4, 2);
46+
expect(triangle.kind()).toEqual('scalene');
47+
});
48+
49+
test('very small triangles are legal', () => {
50+
const triangle = new Triangle(0.4, 0.6, 0.3);
51+
expect(triangle.kind()).toEqual('scalene');
52+
});
53+
54+
test('test triangles with no size are illegal', () => {
55+
const triangle = new Triangle(0, 0, 0);
56+
expect(triangle.kind.bind(triangle)).toThrow();
57+
});
58+
59+
test('triangles with negative sides are illegal', () => {
60+
const triangle = new Triangle(3, 4, -5);
61+
expect(triangle.kind.bind(triangle)).toThrow();
62+
});
63+
64+
test('triangles violating triangle inequality are illegal', () => {
65+
const triangle = new Triangle(1, 1, 3);
66+
expect(triangle.kind.bind(triangle)).toThrow();
67+
});
68+
69+
test('triangles violating triangle inequality are illegal 2', () => {
70+
const triangle = new Triangle(7, 3, 2);
71+
expect(triangle.kind.bind(triangle)).toThrow();
72+
});
73+
74+
test('triangles violating triangle inequality are illegal 3', () => {
75+
const triangle = new Triangle(10, 1, 3);
76+
expect(triangle.kind.bind(triangle)).toThrow();
77+
});
78+
});

0 commit comments

Comments
 (0)