Skip to content

Commit 550cb60

Browse files
committed
add Triangle
1 parent 3eb52f4 commit 550cb60

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
- [Lesson 6 - Sorting](#s_6)
2929
- [MaxProductOfThree](#s_6_max_product_of_three)
3030
- [Distinct](#s_6_distinct)
31+
- [Triangle](#s_6_triangle)
3132
- [Licence](#licence)
3233

3334
<a name="installation"></a>
@@ -270,6 +271,18 @@ https://github.com/talgat-ruby/codility-lessons-javascript/blob/master/lesson6_s
270271

271272
https://app.codility.com/demo/results/trainingBVMXV7-F9S/
272273

274+
<a name="s_6_triangle"></a>
275+
276+
#### Triangle
277+
278+
##### File
279+
280+
https://github.com/talgat-ruby/codility-lessons-javascript/blob/master/lesson6_sorting/Triangle.js
281+
282+
##### Link to Report
283+
284+
https://app.codility.com/demo/results/training62TT8U-DCH/
285+
273286
<a name="licence"></a>
274287

275288
## License

lesson6_sorting/Triangle.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function solution(A) {
2+
const len = A.length;
3+
if (len >= 3) {
4+
A.sort((a, b) => a - b);
5+
for (let i = 2; i < len; i++) {
6+
if (A[i - 2] + A[i - 1] > A[i]) {
7+
return 1;
8+
}
9+
}
10+
}
11+
return 0;
12+
}
13+
14+
module.exports = solution;

lesson6_sorting/Triangle.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const solution = require("./Triangle");
2+
3+
describe("test Triangle", () => {
4+
test("[10, 2, 5, 1, 8, 20] -> 1", () => {
5+
expect(solution([10, 2, 5, 1, 8, 20])).toBe(1);
6+
});
7+
test("[10, 50, 5, 1] -> 0", () => {
8+
expect(solution([10, 50, 5, 1])).toBe(0);
9+
});
10+
test("[1, 1, 1, 1, 5, 5, 5] -> 1", () => {
11+
expect(solution([1, 1, 1, 1, 5, 5, 5])).toBe(1);
12+
});
13+
});

0 commit comments

Comments
 (0)