From bfe727d1ac1d60f035306fd0335f29b5a476d94d Mon Sep 17 00:00:00 2001 From: Nazzoe Date: Sun, 15 Mar 2026 07:24:21 +0000 Subject: [PATCH] Add new power button --- api/controller.js | 1 + public/client.js | 5 ++++- public/default.css | 5 +++++ public/index.html | 6 +++++- test/arithmetic.test.js | 31 +++++++++++++++++++++++++++++-- 5 files changed, 44 insertions(+), 4 deletions(-) diff --git a/api/controller.js b/api/controller.js index 100fc02..63b66d0 100644 --- a/api/controller.js +++ b/api/controller.js @@ -15,6 +15,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 }, + 'power': function(a, b) { return Math.pow(a, b); }, }; if (!req.query.operation) { diff --git a/public/client.js b/public/client.js index 612f1b9..4e912e5 100644 --- a/public/client.js +++ b/public/client.js @@ -32,6 +32,9 @@ function calculate(operand1, operand2, operation) { case '/': uri += "?operation=divide"; break; + case '^': + uri += "?operation=power"; + break; default: setError(); return; @@ -137,7 +140,7 @@ document.addEventListener('keypress', (event) => { numberPressed(event.key); } else if (event.key == '.') { decimalPressed(); - } else if (event.key.match(/^[-*+/]$/)) { + } else if (event.key.match(/^[-*+\/]$/) || event.key == '^') { operationPressed(event.key); } else if (event.key == '=') { equalPressed(); diff --git a/public/default.css b/public/default.css index c652a2b..5ef54f1 100644 --- a/public/default.css +++ b/public/default.css @@ -167,6 +167,11 @@ BODY { grid-column: span 2; } +/* utility class for buttons that should stretch across all four columns */ +.span-4 { + grid-column: span 4; +} + .calculator-buttons { display: grid; grid-template-columns: repeat(4, 1fr); diff --git a/public/index.html b/public/index.html index db09b0b..2c9708d 100644 --- a/public/index.html +++ b/public/index.html @@ -37,8 +37,12 @@ + + - + + + diff --git a/test/arithmetic.test.js b/test/arithmetic.test.js index deded48..448309c 100644 --- a/test/arithmetic.test.js +++ b/test/arithmetic.test.js @@ -93,8 +93,35 @@ describe('Arithmetic', function () { }); }); -// TODO: Challenge #1 - + + // Exponentiation function was added (operation=power) + describe('Exponentiation', function () { + it('raises a positive integer to a positive integer', function (done) { + request.get('/arithmetic?operation=power&operand1=2&operand2=3') + .expect(200) + .end(function (err, res) { + expect(res.body).to.eql({ result: 8 }); + done(); + }); + }); + it('power of zero exponent returns 1', function (done) { + request.get('/arithmetic?operation=power&operand1=5&operand2=0') + .expect(200) + .end(function (err, res) { + expect(res.body).to.eql({ result: 1 }); + done(); + }); + }); + it('power with negative exponent produces fractional result', function (done) { + request.get('/arithmetic?operation=power&operand1=2&operand2=-2') + .expect(200) + .end(function (err, res) { + expect(res.body).to.eql({ result: 0.25 }); + done(); + }); + }); + }); + describe('Multiplication', function () { it('multiplies two positive integers', function (done) {