Skip to content

Commit

Permalink
Calculations: support exponential notation for operands
Browse files Browse the repository at this point in the history
ethomson committed Mar 25, 2019
1 parent 1917e4e commit 4aca9c1
Showing 2 changed files with 20 additions and 4 deletions.
8 changes: 4 additions & 4 deletions api/controllers/arithmeticController.js
Original file line number Diff line number Diff line change
@@ -32,14 +32,14 @@ exports.calculate = function(req, res) {
// Validate operands

if (! req.query.operand1 ||
! req.query.operand1.match(/^(-)?[0-9\.]+$/) ||
req.query.operand1.replace(/[-0-9]/g, '').length > 1) {
! req.query.operand1.match(/^(-)?[0-9\.]+(e(-)?[0-9]+)?$/) ||
req.query.operand1.replace(/[-0-9e]/g, '').length > 1) {
throw new Error("Invalid operand1: " + req.query.operand1);
}

if (! req.query.operand2 ||
! req.query.operand2.match(/^(-)?[0-9\.]+$/) ||
req.query.operand2.replace(/[-0-9]/g, '').length > 1) {
! req.query.operand2.match(/^(-)?[0-9\.]+(e(-)?[0-9]+)?$/) ||
req.query.operand2.replace(/[-0-9e]/g, '').length > 1) {
throw new Error("Invalid operand2: " + req.query.operand2);
}

16 changes: 16 additions & 0 deletions test/arithmetic.js
Original file line number Diff line number Diff line change
@@ -91,6 +91,14 @@ describe('Arithmetic', function() {
done();
});
});
it('adds with negative exponent', function(done) {
request.get('/arithmetic?operation=add&operand1=1.2e-5&operand2=-1.2e-5')
.expect(200)
.end(function(err, res) {
expect(res.body).to.eql({ result: 0 });
done();
});
});
});

describe('Subtraction', function() {
@@ -177,6 +185,14 @@ describe('Arithmetic', function() {
done();
});
});
it('multiplies supporting exponential notation', function(done) {
request.get('/arithmetic?operation=multiply&operand1=4.2e1&operand2=1e0')
.expect(200)
.end(function(err, res) {
expect(res.body).to.eql({ result: 42 });
done();
});
});
});

describe('Division', function() {

0 comments on commit 4aca9c1

Please sign in to comment.