Codility: Triangle Solution

Lesson 6 Sorting
1function solution(A) {
2 if (A.length < 3) {
3 return 0
4 }
5
6 function isTriangle(a, b, c) {
7 return (a > 0) & (b > 0) & (c > 0) && a + b > c && a + c > b && b + c > a
8 }
9
10 A.sort((a, b) => a - b)
11
12 let count = 0
13 for (let i = 0; i < A.length - 2; i++) {
14 if (!isTriangle(A[i], A[i + 1], A[i + 2])) {
15 count++
16 }
17 }
18
19 return count === A.length - 2 ? 0 : 1
20}

Comments

Loading comments...

Tags

codility

Apply and earn a $2,500 bonus once you're hired on your first job!

Clients from the Fortune 500 to Silicon Valley startups

Choose your own rate, get paid on time

From hourly, part-time, to full-time positions

Flexible remote working environment

A lot of open JavaScript jobs!!

Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.

Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.

Next Post

Codility: NumberOfDiscIntersections

Lesson 6 Sorting

HoningJS

Search Posts