Published at Jul 13 2018
·
0 comments

Instructions

Test suite

Solution

A Pythagorean triplet is a set of three natural numbers, {a, b, c}, for which,

```
a**2 + b**2 = c**2
```

For example,

```
3**2 + 4**2 = 9 + 16 = 25 = 5**2.
```

There exists exactly one Pythagorean triplet for which a + b + c = 1000.

Find the product a * b * c.

Go through the setup instructions for JavaScript to install the necessary dependencies:

http://exercism.io/languages/javascript/installation

The provided test suite uses Jasmine. You can install it by opening a terminal window and running the following command:

```
npm install -g jasmine
```

Run the test suite from the exercise directory with:

```
jasmine pythagorean-triplet.spec.js
```

In many test suites all but the first test have been marked "pending".
Once you get a test passing, activate the next one by changing `xit`

to `it`

.

Problem 9 at Project Euler http://projecteuler.net/problem=9

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

```
var Triplet = require('./pythagorean-triplet');
describe('Triplet', function () {
it('calculates the sum', function () {
expect(new Triplet(3, 4, 5).sum()).toBe(12);
});
xit('calculates the product', function () {
expect(new Triplet(3, 4, 5).product()).toBe(60);
});
xit('can recognize a pythagorean triplet', function () {
expect(new Triplet(3, 4, 5).isPythagorean()).toBe(true);
});
xit('can recognize a non pythagorean triplet', function () {
expect(new Triplet(5, 6, 7).isPythagorean()).toBe(false);
});
xit('can make triplets up to 10', function () {
var triplets = Triplet.where({ maxFactor: 10 });
var products = triplets.sort().map( function (triplet) {
return triplet.product();
});
expect(products).toEqual([60, 480]);
});
xit('can make triplets 11 through 20', function () {
var triplets = Triplet.where({ minFactor: 11, maxFactor: 20 });
var products = triplets.sort().map( function (triplet) {
return triplet.product();
});
expect(products).toEqual([3840]);
});
xit('can filter on sum', function () {
var triplets = Triplet.where({ sum: 180, maxFactor: 100 });
var products = triplets.sort().map( function (triplet) {
return triplet.product();
});
expect(products).toEqual([118080, 168480, 202500]);
});
});
```

```
function Triplet(a,b,c) {
var abc = [a,b,c].sort();
this.sum = function(){ return a+b+c }
this.product = function(){ return a*b*c }
this.isPythagorean = function(){ return (c*c==a*a+b*b) }
}
Triplet.where = function(args){
var triplets=[];
args.minFactor|=1;
for (var c=args.maxFactor; c>=(args.minFactor||5); --c) {
for (var b=c-1; b>=(args.minFactor||c/2); --b) {
var a = Math.sqrt(c*c - b*b);
if ((a<b) && (a>=args.minFactor) && (a==parseInt(a))) {
if (args.sum) {
if ((a+b+c)==args.sum) triplets.push(new Triplet(a,b,c));
} else {
triplets.unshift(new Triplet(a,b,c));
}
}
}
}
return triplets;
}
module.exports=Triplet;
```

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

- What compromises have been made?
- Are there new concepts here that you could read more about to improve your understanding?

Level up your programming skills with 3,373 exercises across 50 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments