Skip to content
Draft
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
1 change: 1 addition & 0 deletions api/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ exports.calculate = function(req, res) {
'subtract': function(a, b) { return a - b },
'multiply': function(a, b) { return a * b },
'divide': function(a, b) { return a / b },
'modulo': function(a, b) { return a % b },
};

if (!req.query.operation) {
Expand Down
53 changes: 53 additions & 0 deletions test/arithmetic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@ describe('Arithmetic', function () {

// TODO: Challenge #1

describe('Subtraction', function () {
it('subtracts a positive integer from a positive integer', function (done) {
request.get('/arithmetic?operation=subtract&operand1=42&operand2=21')
.expect(200)
.end(function (err, res) {
expect(res.body).to.eql({ result: 21 });
done();
});
});
});

describe('Multiplication', function () {
it('multiplies two positive integers', function (done) {
Expand Down Expand Up @@ -205,4 +215,47 @@ describe('Arithmetic', function () {
});
});
});

describe('Modulo', function () {
it('computes modulo of two positive integers', function (done) {
request.get('/arithmetic?operation=modulo&operand1=42&operand2=5')
.expect(200)
.end(function (err, res) {
expect(res.body).to.eql({ result: 2 });
done();
});
});
it('computes modulo with a result of zero', function (done) {
request.get('/arithmetic?operation=modulo&operand1=42&operand2=21')
.expect(200)
.end(function (err, res) {
expect(res.body).to.eql({ result: 0 });
done();
});
});
it('computes modulo of a negative integer', function (done) {
request.get('/arithmetic?operation=modulo&operand1=-42&operand2=5')
.expect(200)
.end(function (err, res) {
expect(res.body).to.eql({ result: -2 });
done();
});
});
it('computes modulo with floating point numbers', function (done) {
request.get('/arithmetic?operation=modulo&operand1=5.5&operand2=2')
.expect(200)
.end(function (err, res) {
expect(res.body).to.eql({ result: 1.5 });
done();
});
});
it('computes modulo where divisor is larger than dividend', function (done) {
request.get('/arithmetic?operation=modulo&operand1=5&operand2=42')
.expect(200)
.end(function (err, res) {
expect(res.body).to.eql({ result: 5 });
done();
});
});
});
});