Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add /triplets route and tests #77

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions routes/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,4 +361,84 @@ router.post('/armstrong',(req,res)=>{
}
});

/*
bool <= (int[] sides)
The function takes array of three sides of a triangle
and returns a JSON output stating if the given sides together
form a right angled triangle
eg:
body = {
"sides": [3, 4, 5]
}
*/
router.post('/triplets', (req, res) => {
try {
let sides = req.body.sides;

if (sides.length != 3) {
return res.status(400).json({
"Error": "Input must be an array of integers of length 3 with key named 'sides'."
});
}

sides = sides.map(side => parseInt(side));

gcd = (a, b) => {
while(b != 0) {
a = a % b;
b = a / b;
}
return a;
};

sides.sort();
// Check: If the length of the side is a non-zero positive integer
if (sides[0] > 0) {
a = sides[0]
b = sides[1]
c = sides[2]

// Check: Sum of any two sides of a triangle must be greater than the third side
if (a + b > c) {
// VALID triangle
p1 = a; p2 = c - b;
div = gcd(p1, p2);
p1 /= div;
p2 /= div;

q1 = c + b; q2 = b;
div = gcd(q1, q2);
q1 /= div;
q2 /= div;

if (p1 == q1 && p2 == q2) {
return res.status(200).json({
result: "Yes. Given sides form a right angled triangle.",
sides: sides,
});
}
else {
return res.status(200).json({
result: "No. Given sides don't form a right angled triangle.",
sides: sides,
});
}

}

}
return res.status(200).json({
result: "No. Given sides don't form a triangle.",
sides: sides,
});
}
catch(e) {
return res.status(400).json({
"Error": "Input must be an array of integers of length 3 with key named 'sides'."
});
}

});


module.exports = router;
82 changes: 82 additions & 0 deletions tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,87 @@ describe("Starting tests for app.js ->", () => {
});
});

it("To check if /triplets endpoint is working; is a right angled triangle",(done)=>{
const sides = [3, 4, 5];
chai
.request(app)
.post("/triplets")
.type("application/json")
.send({sides: sides})
.end((err, res) => {
if (err) {
done(err);
process.exit(1);
}
else {
res.should.have.status(200);
res.body.should.have.property('result');
res.body.should.have.property('result', "Yes. Given sides form a right angled triangle.");
done();
}
});
});

it("To check if /triplets endpoint is working; is not a right angled triangle",(done)=>{
const sides = [3, 5, 5];
chai
.request(app)
.post("/triplets")
.type("application/json")
.send({sides: sides})
.end((err, res) => {
if (err) {
done(err);
process.exit(1);
}
else {
res.should.have.status(200);
res.body.should.have.property('result');
res.body.should.have.property('result', "No. Given sides don't form a right angled triangle.");
done();
}
});
});

it("To check if /triplets endpoint is working; is not a triangle",(done)=>{
const sides = [1, 4, 5];
chai
.request(app)
.post("/triplets")
.type("application/json")
.send({sides: sides})
.end((err, res) => {
if (err) {
done(err);
process.exit(1);
}
else {
res.should.have.status(200);
res.body.should.have.property('result');
res.body.should.have.property('result', "No. Given sides don't form a triangle.");
done();
}
});
});

it("To check if /triplets endpoint is working; bad credentials",(done)=>{
const sides = 12;
chai
.request(app)
.post("/triplets")
.type("application/json")
.send({sides: sides})
.end((err, res) => {
if (err) {
done(err);
process.exit(1);
}
else {
res.should.have.status(400);
res.body.should.have.property('Error');
done();
}
});
});
//Add more tests here
});