From efa9bb1c5434696c6a21f0c3b0f98223ceed5d28 Mon Sep 17 00:00:00 2001 From: Rezwan Ibne Borhan <122799034+rezwanBorhan@users.noreply.github.com> Date: Sat, 14 Mar 2026 18:26:23 +0000 Subject: [PATCH 1/4] Add power operation and corresponding UI button; implement subtraction tests --- api/controller.js | 1 + public/client.js | 3 +++ public/index.html | 2 ++ test/arithmetic.test.js | 54 ++++++++++++++++++++++++++++++++++++++++- 4 files changed, 59 insertions(+), 1 deletion(-) diff --git a/api/controller.js b/api/controller.js index 949731c..70feb97 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 }, + '^': function(a, b) { return Math.pow(a, b) } }; if (!req.query.operation) { diff --git a/public/client.js b/public/client.js index 1c60f86..233596b 100644 --- a/public/client.js +++ b/public/client.js @@ -33,6 +33,9 @@ function calculate(operand1, operand2, operation) { case '/': uri += "?operation=divide"; break; + case '^': + uri += "?operation=^"; + break; default: setError(); return; diff --git a/public/index.html b/public/index.html index 400c454..3a31f77 100644 --- a/public/index.html +++ b/public/index.html @@ -41,6 +41,8 @@ + + diff --git a/test/arithmetic.test.js b/test/arithmetic.test.js index deded48..5275352 100644 --- a/test/arithmetic.test.js +++ b/test/arithmetic.test.js @@ -94,7 +94,59 @@ describe('Arithmetic', function () { }); // TODO: Challenge #1 - +// add tests for subtraction, multiplication, and division + + describe('Subtraction', function () { + it('subtracts two positive integers', function (done) { + request.get('/arithmetic?operation=subtract&operand1=21&operand2=2') + .expect(200) + .end(function (err, res) { + expect(res.body).to.eql({ result: 19 }); + done(); + }); + }); + it('subtracts a positive integer with zero', function (done) { + request.get('/arithmetic?operation=subtract&operand1=21&operand2=0') + .expect(200) + .end(function (err, res) { + expect(res.body).to.eql({ result: 21 }); + done(); + }); + }); + it('subtracts a positive integer and negative integer', function (done) { + request.get('/arithmetic?operation=subtract&operand1=21&operand2=-2') + .expect(200) + .end(function (err, res) { + expect(res.body).to.eql({ result: 23 }); + done(); + }); + }); + it('subtracts two negative integers', function (done) { + request.get('/arithmetic?operation=subtract&operand1=-21&operand2=-2') + .expect(200) + .end(function (err, res) { + expect(res.body).to.eql({ result: -19 }); + done(); + }); + }); + it('subtracts two floating point numbers', function (done) { + request.get('/arithmetic?operation=subtract&operand1=.5&operand2=0.5') + .expect(200) + .end(function (err, res) { + expect(res.body).to.eql({ result: 0 }); + done(); + }); + }); + it('subtracts supporting exponential notation', function (done) { + request.get('/arithmetic?operation=subtract&operand1=4.2e1&operand2=1e0') + .expect(200) + .end(function (err, res) { + expect(res.body).to.eql({ result: 41 }); + done(); + }); + }); + }); + describe('Multiplication', function () { it('multiplies two positive integers', function (done) { From d01e65eeed1916c5d4becc649ba6d8efd4f0f97b Mon Sep 17 00:00:00 2001 From: Rezwan Ibne Borhan <122799034+rezwanBorhan@users.noreply.github.com> Date: Sat, 14 Mar 2026 18:35:18 +0000 Subject: [PATCH 2/4] Add tests for power operation in arithmetic module --- test/arithmetic.test.js | 52 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/test/arithmetic.test.js b/test/arithmetic.test.js index 5275352..3b8906d 100644 --- a/test/arithmetic.test.js +++ b/test/arithmetic.test.js @@ -257,4 +257,56 @@ describe('Arithmetic', function () { }); }); }); + + // add tests for power (or exponential) function + describe('Power', function () { + it('raises a positive integer to a positive integer power', function (done) { + request.get('/arithmetic?operation=^&operand1=2&operand2=3') + .expect(200) + .end(function (err, res) { + expect(res.body).to.eql({ result: 8 }); + done(); + }); + }); + it('raises a positive integer to the zero power', function (done) { + request.get('/arithmetic?operation=^&operand1=2&operand2=0') + .expect(200) + .end(function (err, res) { + expect(res.body).to.eql({ result: 1 }); + done(); + }); + }); + it('raises a positive integer to a negative integer power', function (done) { + request.get('/arithmetic?operation=^&operand1=2&operand2=-3') + .expect(200) + .end(function (err, res) { + expect(res.body).to.eql({ result: 0.125 }); + done(); + }); + }); + it('raises a negative integer to a positive integer power', function (done) { + request.get('/arithmetic?operation=^&operand1=-2&operand2=3') + .expect(200) + .end(function (err, res) { + expect(res.body).to.eql({ result: -8 }); + done(); + }); + }); + it('raises a negative integer to an even positive integer power', function (done) { + request.get('/arithmetic?operation=^&operand1=-2&operand2=4') + .expect(200) + .end(function (err, res) { + expect(res.body).to.eql({ result: 16 }); + done(); + }); + }); + it('raises zero to a positive integer power', function (done) { + request.get('/arithmetic?operation=^&operand1=0&operand2=3') + .expect(200) + .end(function (err, res) { + expect(res.body).to.eql({ result: 0 }); + done(); + }); + }); + }); }); From 1da2dbc42921903bbdbc5966fe8b5528d316b127 Mon Sep 17 00:00:00 2001 From: Rezwan Ibne Borhan <122799034+rezwanBorhan@users.noreply.github.com> Date: Sat, 14 Mar 2026 18:47:24 +0000 Subject: [PATCH 3/4] Implement modulo operation and corresponding tests; add UI button for modulo --- api/controller.js | 1 + public/client.js | 15 ++++++++++++++ public/index.html | 3 ++- test/arithmetic.test.js | 43 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 61 insertions(+), 1 deletion(-) diff --git a/api/controller.js b/api/controller.js index 70feb97..e4066d9 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 }, '^': function(a, b) { return Math.pow(a, b) } }; diff --git a/public/client.js b/public/client.js index 233596b..8446545 100644 --- a/public/client.js +++ b/public/client.js @@ -33,6 +33,9 @@ function calculate(operand1, operand2, operation) { case '/': uri += "?operation=divide"; break; + case '%': + uri += "?operation=modulo"; + break; case '^': uri += "?operation=^"; break; @@ -146,6 +149,18 @@ document.addEventListener('keypress', (event) => { } else if (event.key == '=') { equalPressed(); } + // add additional key press logics for rest of the operations, such as power (or exponential) function + else if (event.key == '^') { + operationPressed(event.key); + } else if (event.key == '%') { + operationPressed(event.key); + } else if (event.key == 'c') { + clearPressed(); + } else if (event.key == 's') { + signPressed(); + } else if (event.key == 'e') { + clearEntryPressed(); + } }); function getValue() { diff --git a/public/index.html b/public/index.html index 3a31f77..8c59ca1 100644 --- a/public/index.html +++ b/public/index.html @@ -16,7 +16,8 @@