From e4ccdfad5f5c16f7d0263fb0009b24281e2df52e Mon Sep 17 00:00:00 2001 From: Carina <carina.oliveira-inacio@etu.hesge.ch> Date: Mon, 18 Nov 2019 14:41:44 +0100 Subject: [PATCH] =?UTF-8?q?g=20f=C3=A9=201=20comite=20lol?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- compute.js | 16 ++++++++++++++++ routes/index.js | 14 ++++++++++---- tests/compute.test.js | 16 +++++++++++++++- views/main-content.pug | 8 +++++--- 4 files changed, 46 insertions(+), 8 deletions(-) diff --git a/compute.js b/compute.js index 6d323b2..7b2db99 100644 --- a/compute.js +++ b/compute.js @@ -20,6 +20,22 @@ function Compute() { * @returns {Number} Difference between a and b (a - b) */ this.sub = (a, b) => Number(a) - Number(b); + /** + * @method sub + * @desc Calculate the difference between two numbers + * @param {Number} a First member of the operation + * @param {Number} b Second member of the operation + * @returns {Number} Difference between a and b (a - b) + */ + this.mul = (a, b) => Number(a) * Number(b); + /** + * @method sub + * @desc Calculate the difference between two numbers + * @param {Number} a First member of the operation + * @param {Number} b Second member of the operation + * @returns {Number} Difference between a and b (a - b) + */ + this.div = (a, b) => Number(a) / Number(b); } module.exports = {Compute} diff --git a/routes/index.js b/routes/index.js index 3c8531c..b20e297 100644 --- a/routes/index.js +++ b/routes/index.js @@ -18,7 +18,7 @@ router.get('/', async (req, res) => { router.post('/', [ body('a').isInt().withMessage('"a" must be a number'), body('b').isInt().withMessage('"b" must be a number'), - body('operator').isIn(["+", "-", "x"]).withMessage('Operator must be "+", "-" or "x"'), + body('operator').isIn(["+", "-", "*", "/","x"]).withMessage('Operator must be "+", "-" or "x"'), ], async (req, res) => { // preprare return body @@ -42,7 +42,13 @@ router.post('/', [ break; case "-": result = compute.sub(a, b); - break; + break; + case "*": + result = compute.mul(a, b); + break; + case "/": + result = compute.div(a, b); + break; default: break; } @@ -58,7 +64,7 @@ router.post('/', [ await operation.save() .then(() => {}) - .catch(() => { + .catch(() => { resultBodyContent.errors.push("Can't save data in database!"); }); } else { @@ -74,4 +80,4 @@ async function getOperationsList() { return await Operation.find().sort({"created_at": -1}); } -module.exports = router; \ No newline at end of file +module.exports = router; diff --git a/tests/compute.test.js b/tests/compute.test.js index f50b943..603f79a 100644 --- a/tests/compute.test.js +++ b/tests/compute.test.js @@ -14,4 +14,18 @@ test.each([[1, 1, 0], [-1, 1, -2], [5, 3, 2]])( '%i - %i equals %i', (a, b, expected) => { expect(operation.sub(a, b)).toBe(expected); } -); \ No newline at end of file +); + +// test Compute.sub function +test.each([[1, 1, 1], [-1, 1, -1], [5, 3, 5/3]])( + '%i / %i equals %i', (a, b, expected) => { + expect(operation.div(a, b)).toBe(expected); + } +); + +// test Compute.sub function +test.each([[1, 1, 1], [-1, 1, -1], [5, 3, 15]])( + '%i - %i equals %i', (a, b, expected) => { + expect(operation.mul(a, b)).toBe(expected); + } +); diff --git a/views/main-content.pug b/views/main-content.pug index e341f95..86a6f4b 100644 --- a/views/main-content.pug +++ b/views/main-content.pug @@ -38,14 +38,16 @@ block content td(colspan=2 class="opr-btns") input(type="submit" value="+" class="btn") input(type="submit" value="-" class="btn") + input(type="submit" value="*" class="btn") + input(type="submit" value="/" class="btn") input(type="hidden" name="operator" id="operator") h3= result td.history-td - div + div strong History (from mongo database) - div.history-container + div.history-container if operations.length table.history-table each operation in operations @@ -53,4 +55,4 @@ block content td.datetime-td !{moment(operation.created_at).format('MMMM Do YYYY, h:mm:ss a')} td #{operation.a} #{operation.operator} #{operation.b} = #{operation.result} else - | No history yet \ No newline at end of file + | No history yet -- GitLab