diff --git a/api/controller.js b/api/controller.js index 949731c..adf878a 100644 --- a/api/controller.js +++ b/api/controller.js @@ -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) { diff --git a/test/arithmetic.test.js b/test/arithmetic.test.js index deded48..b4eca1a 100644 --- a/test/arithmetic.test.js +++ b/test/arithmetic.test.js @@ -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) { @@ -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(); + }); + }); + }); });