diff --git a/compute.js b/compute.js index 6d323b2b6da64ce9c5cdd29158757a83e168adf2..7b2db993afa5c266dbb0330179529eeb0a93dc3e 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 3c8531cb74fb67fc97915da72bc23d8b76393b42..b20e297afd3d298dbbf16d11eaddb76f1564e405 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 f50b9439950ce237fd4a8c545e6af5c6b46c34eb..603f79a51a9ec118ff2c96245bd7c79e0df1031e 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 e341f955242ceca38ea58f840a6dbd50f2a4d706..86a6f4b8dd23ea7923668a61eac3a5bea5e924ac 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