From 63f10f9aa915001dfc09428facff45f75855c837 Mon Sep 17 00:00:00 2001 From: "fred.meng" Date: Mon, 15 Jul 2024 11:54:31 +0000 Subject: [PATCH 1/8] =?UTF-8?q?=E5=9C=A8=E8=AE=A1=E7=AE=97=E5=99=A8?= =?UTF-8?q?=E4=B8=AD=E5=A2=9E=E5=8A=A0=E5=B9=B3=E6=96=B9=E7=9A=84=E7=AE=97?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/controller.js | 1 + public/client.js | 3 +++ public/index.html | 2 ++ 3 files changed, 6 insertions(+) diff --git a/api/controller.js b/api/controller.js index 949731c..a9efaf1 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 }, + 'power': function(a, b) { return Math.pow(a, b) }, }; if (!req.query.operation) { diff --git a/public/client.js b/public/client.js index 1c60f86..1227a95 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=power"; + break; default: setError(); return; diff --git a/public/index.html b/public/index.html index 400c454..1fd7a8d 100644 --- a/public/index.html +++ b/public/index.html @@ -41,6 +41,8 @@ + + From 910dbcc6444fb6bf250983e8d71af538c248ee64 Mon Sep 17 00:00:00 2001 From: "fred.meng" Date: Tue, 16 Jul 2024 01:34:06 +0000 Subject: [PATCH 2/8] =?UTF-8?q?=E6=9B=B4=E6=96=B0Container=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=EF=BC=8C=E5=8C=85=E6=8B=AC=EF=BC=9Ahttp=5Fproxy?= =?UTF-8?q?=EF=BC=8Chttps=5Fproxy?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .devcontainer/devcontainer.json | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index ffdd4d3..acd8364 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -15,6 +15,12 @@ ] } }, + "containerEnv": { + "HTTP_PROXY": "${localEnv:HTTP_PROXY}", + "HTTPS_PROXY": "${localEnv:HTTP_PROXY}", + "http_proxy": "${localEnv:HTTP_PROXY}", + "https_proxy": "${localEnv:HTTP_PROXY}" + }, // Features to add to the dev container. More info: https://containers.dev/features. // "features": {}, From 16edcb7224d8aff8ba2957128b87a0929c2b200d Mon Sep 17 00:00:00 2001 From: "fred.meng" Date: Tue, 16 Jul 2024 01:38:01 +0000 Subject: [PATCH 3/8] =?UTF-8?q?=E6=9B=B4=E6=96=B0DevContainer=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=EF=BC=8C=E5=B0=86Proxy=E8=AE=BE=E7=BD=AE=E4=B8=BA?= =?UTF-8?q?=EF=BC=9Ahttp://host.docker.internal:7890?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .devcontainer/devcontainer.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index acd8364..ab41ccb 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -16,10 +16,10 @@ } }, "containerEnv": { - "HTTP_PROXY": "${localEnv:HTTP_PROXY}", - "HTTPS_PROXY": "${localEnv:HTTP_PROXY}", - "http_proxy": "${localEnv:HTTP_PROXY}", - "https_proxy": "${localEnv:HTTP_PROXY}" + "HTTP_PROXY": "http://host.docker.internal:7890", + "HTTPS_PROXY": "http://host.docker.internal:7890", + "http_proxy": "http://host.docker.internal:7890", + "https_proxy": "http://host.docker.internal:7890" }, // Features to add to the dev container. More info: https://containers.dev/features. From 2bd7ae50e3951471e35e25a0761d0ee96ac5a23a Mon Sep 17 00:00:00 2001 From: "fred.meng" Date: Tue, 16 Jul 2024 03:06:20 +0000 Subject: [PATCH 4/8] =?UTF-8?q?=E6=9B=B4=E6=96=B0DevContainer=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=EF=BC=8C=E5=A2=9E=E5=8A=A0Docker=20Start=E5=90=8E?= =?UTF-8?q?=E5=AF=B9git=20proxy=E7=9A=84=E8=AE=BE=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .devcontainer/devcontainer.json | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index ab41ccb..d9c940c 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -15,9 +15,13 @@ ] } }, + // Set *default* container specific shell values on container create. + "settings": { + "terminal.integrated.shell.linux": "/bin/bash" + }, + + // Add the proxy settings to the container. "containerEnv": { - "HTTP_PROXY": "http://host.docker.internal:7890", - "HTTPS_PROXY": "http://host.docker.internal:7890", "http_proxy": "http://host.docker.internal:7890", "https_proxy": "http://host.docker.internal:7890" }, @@ -29,7 +33,10 @@ "forwardPorts": [3000], // Use 'postCreateCommand' to run commands after the container is created. - "postCreateCommand": "npm install" + "postCreateCommand": "npm install", + + // Use 'postStartCommand' to run git proxy configuration after the container is started. + "postStartCommand": "git config --global http.proxy http://host.docker.internal:7890" // Configure tool-specific properties. // "customizations": {}, From 9af24d617e2a78044c88e43b195b714f6adb9cbe Mon Sep 17 00:00:00 2001 From: "fred.meng" Date: Tue, 16 Jul 2024 03:08:13 +0000 Subject: [PATCH 5/8] =?UTF-8?q?=E5=B0=86Setting=E7=A7=BB=E5=85=A5=E5=88=B0?= =?UTF-8?q?customizations=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .devcontainer/devcontainer.json | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index d9c940c..c1fd916 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -12,13 +12,12 @@ "extensions": [ "GitHub.copilot", "GitHub.copilot-labs" - ] + ], + "settings": { + "terminal.integrated.shell.linux": "/bin/bash" + } } }, - // Set *default* container specific shell values on container create. - "settings": { - "terminal.integrated.shell.linux": "/bin/bash" - }, // Add the proxy settings to the container. "containerEnv": { From da930adfa3fde4cd2f402308967d65ea97b83838 Mon Sep 17 00:00:00 2001 From: "fred.meng" Date: Tue, 16 Jul 2024 14:21:13 +0000 Subject: [PATCH 6/8] Challenge #1, add tests for subtraction --- test/arithmetic.test.js | 54 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 52 insertions(+), 2 deletions(-) diff --git a/test/arithmetic.test.js b/test/arithmetic.test.js index deded48..7387c9a 100644 --- a/test/arithmetic.test.js +++ b/test/arithmetic.test.js @@ -94,8 +94,58 @@ describe('Arithmetic', function () { }); // TODO: Challenge #1 - - +// add tests for subtraction + describe('Subtraction', function () { + it('subtracts two positive integers', function (done) { + request.get('/arithmetic?operation=subtract&operand1=21&operand2=18') + .expect(200) + .end(function (err, res) { + expect(res.body).to.eql({ result: 3 }); + done(); + }); + }); + it('subtracts a positive integer from zero', function (done) { + request.get('/arithmetic?operation=subtract&operand1=0&operand2=21') + .expect(200) + .end(function (err, res) { + expect(res.body).to.eql({ result: -21 }); + done(); + }); + }); + it('subtracts a negative integer from a positive integer', function (done) { + request.get('/arithmetic?operation=subtract&operand1=21&operand2=-42') + .expect(200) + .end(function (err, res) { + expect(res.body).to.eql({ result: 63 }); + done(); + }); + }); + it('subtracts two negative integers', function (done) { + request.get('/arithmetic?operation=subtract&operand1=-21&operand2=-21') + .expect(200) + .end(function (err, res) { + expect(res.body).to.eql({ result: 0 }); + done(); + }); + }); + it('subtracts a floating point number from an integer', function (done) { + request.get('/arithmetic?operation=subtract&operand1=2.5&operand2=1.5') + .expect(200) + .end(function (err, res) { + expect(res.body).to.eql({ result: 1 }); + done(); + }); + }); + it('subtracts with negative exponent', function (done) { + request.get('/arithmetic?operation=subtract&operand1=1.2e-5&operand2=-1.2e-5') + .expect(200) + .end(function (err, res) { + expect(res.body).to.eql({ result: 2.4e-5 }); + done(); + }); + }); + }); + describe('Multiplication', function () { it('multiplies two positive integers', function (done) { request.get('/arithmetic?operation=multiply&operand1=21&operand2=2') From 98c70d626ca4385318f3da0d6ae7fd018f1903e5 Mon Sep 17 00:00:00 2001 From: "fred.meng" Date: Wed, 17 Jul 2024 05:40:36 +0000 Subject: [PATCH 7/8] completed challenge #2: add tests for power function --- test/arithmetic.test.js | 166 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 165 insertions(+), 1 deletion(-) diff --git a/test/arithmetic.test.js b/test/arithmetic.test.js index 7387c9a..513983e 100644 --- a/test/arithmetic.test.js +++ b/test/arithmetic.test.js @@ -145,7 +145,171 @@ describe('Arithmetic', function () { }); }); }); - + +// add tests for power + describe('Power', function () { + it('raises a positive integer to a positive integer power', 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('raises a positive integer to a zero power', function (done) { + request.get('/arithmetic?operation=power&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=power&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=power&operand1=-2&operand2=3') + .expect(200) + .end(function (err, res) { + expect(res.body).to.eql({ result: -8 }); + done(); + }); + }); + it('raises a negative integer to a zero power', function (done) { + request.get('/arithmetic?operation=power&operand1=-2&operand2=0') + .expect(200) + .end(function (err, res) { + expect(res.body).to.eql({ result: 1 }); + done(); + }); + }); + it('raises a negative integer to a negative integer power', function (done) { + request.get('/arithmetic?operation=power&operand1=-2&operand2=-3') + .expect(200) + .end(function (err, res) { + expect(res.body).to.eql({ result: -0.125 }); + done(); + }); + }); + it('raises a floating point number to a positive integer power', function (done) { + request.get('/arithmetic?operation=power&operand1=2.5&operand2=2') + .expect(200) + .end(function (err, res) { + expect(res.body).to.eql({ result: 6.25 }); + done(); + }); + }); + it('raises a floating point number to a zero power', function (done) { + request.get('/arithmetic?operation=power&operand1=2.5&operand2=0') + .expect(200) + .end(function (err, res) { + expect(res.body).to.eql({ result: 1 }); + done(); + }); + }); + it('raises a floating point number to a negative integer power', function (done) { + request.get('/arithmetic?operation=power&operand1=2.5&operand2=-2') + .expect(200) + .end(function (err, res) { + expect(res.body).to.eql({ result: 0.16 }); + done(); + }); + }); + it('raises a negative floating point number to a positive integer power', function (done) { + request.get('/arithmetic?operation=power&operand1=-2.5&operand2=2') + .expect(200) + .end(function (err, res) { + expect(res.body).to.eql({ result: 6.25 }); + done(); + }); + }); + it('raises a negative floating point number to a zero power', function (done) { + request.get('/arithmetic?operation=power&operand1=-2.5&operand2=0') + .expect(200) + .end(function (err, res) { + expect(res.body).to.eql({ result: 1 }); + done(); + }); + }); + it('raises a negative floating point number to a negative integer power', function (done) { + request.get('/arithmetic?operation=power&operand1=-2.5&operand2=-2') + .expect(200) + .end(function (err, res) { + expect(res.body).to.eql({ result: 0.16 }); + done(); + }); + }); + it('raises a positive integer to a floating point number power', function (done) { + request.get('/arithmetic?operation=power&operand1=2&operand2=0.5') + .expect(200) + .end(function (err, res) { + expect(res.body).to.eql({ result: 1.4142135623730951 }); + done(); + }); + }); + it('raises a positive integer to a negative floating point number power', function (done) { + request.get('/arithmetic?operation=power&operand1=2&operand2=-0.5') + .expect(200) + .end(function (err, res) { + expect(res.body).to.eql({ result: 0.7071067811865475 }); + done(); + }); + }); + it('raises a negative integer to a floating point number power', function (done) { + request.get('/arithmetic?operation=power&operand1=-2&operand2=0.5') + .expect(200) + .end(function (err, res) { + expect(res.body).to.eql({ result: null }); + done(); + }); + }); + it('raises a negative integer to a negative floating point number power', function (done) { + request.get('/arithmetic?operation=power&operand1=-2&operand2=-0.5') + .expect(200) + .end(function (err, res) { + expect(res.body).to.eql({ result: null }); + done(); + }); + }); + it('raises a floating point number to a floating point number power', function (done) { + request.get('/arithmetic?operation=power&operand1=2.5&operand2=0.5') + .expect(200) + .end(function (err, res) { + expect(res.body).to.eql({ result: 1.5811388300841898 }); + done(); + }); + }); + it('raises a floating point number to a negative floating point number power', function (done) { + request.get('/arithmetic?operation=power&operand1=2.5&operand2=-0.5') + .expect(200) + .end(function (err, res) { + expect(res.body).to.eql({ result: 0.6324555320336759 }); + done(); + }); + }); + it('raises a negative floating point number to a floating point number power', function (done) { + request.get('/arithmetic?operation=power&operand1=-2.5&operand2=0.5') + .expect(200) + .end(function (err, res) { + expect(res.body).to.eql({ result: null }); + done(); + }); + }); + it('raises a negative floating point number to a negative floating point number power', function (done) { + request.get('/arithmetic?operation=power&operand1=-2.5&operand2=-0.5') + .expect(200) + .end(function (err, res) { + expect(res.body).to.eql({ result: null }); + done(); + }); + }); + }); + describe('Multiplication', function () { it('multiplies two positive integers', function (done) { request.get('/arithmetic?operation=multiply&operand1=21&operand2=2') From a0a5848c3897cefd03e0283742977212941ac54f Mon Sep 17 00:00:00 2001 From: "fred.meng" Date: Wed, 17 Jul 2024 07:59:04 +0000 Subject: [PATCH 8/8] completed all challenges for the exercise --- api/controller.js | 4 +++- public/client.js | 3 +++ test/arithmetic.test.js | 44 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/api/controller.js b/api/controller.js index a9efaf1..4a88319 100644 --- a/api/controller.js +++ b/api/controller.js @@ -16,7 +16,9 @@ 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) }, + 'power': function(a, b) { return Math.pow(a, b) }, + //add new operator for square root + 'sqrt': function(a, b) { return Math.sqrt(a) }, }; if (!req.query.operation) { diff --git a/public/client.js b/public/client.js index 1227a95..12ea7c0 100644 --- a/public/client.js +++ b/public/client.js @@ -36,6 +36,9 @@ function calculate(operand1, operand2, operation) { case '^': uri += "?operation=power"; break; + case 'sqrt': + uri += "?operation=sqrt"; + break; default: setError(); return; diff --git a/test/arithmetic.test.js b/test/arithmetic.test.js index 513983e..a3943b9 100644 --- a/test/arithmetic.test.js +++ b/test/arithmetic.test.js @@ -310,6 +310,50 @@ describe('Arithmetic', function () { }); }); + // add tests for square root + describe('Square Root', function () { + it('finds the square root of a positive integer', function (done) { + request.get('/arithmetic?operation=sqrt&operand1=25&operand2=0') + .expect(200) + .end(function (err, res) { + expect(res.body).to.eql({ result: 5 }); + done(); + }); + }); + it('finds the square root of zero', function (done) { + request.get('/arithmetic?operation=sqrt&operand1=0&operand2=0') + .expect(200) + .end(function (err, res) { + expect(res.body).to.eql({ result: 0 }); + done(); + }); + }); + it('finds the square root of a negative integer', function (done) { + request.get('/arithmetic?operation=sqrt&operand1=-25&operand2=0') + .expect(200) + .end(function (err, res) { + expect(res.body).to.eql({ result: null }); + done(); + }); + }); + it('finds the square root of a floating point number', function (done) { + request.get('/arithmetic?operation=sqrt&operand1=2.5&operand2=0') + .expect(200) + .end(function (err, res) { + expect(res.body).to.eql({ result: 1.5811388300841898 }); + done(); + }); + }); + it('finds the square root of a negative floating point number', function (done) { + request.get('/arithmetic?operation=sqrt&operand1=-2.5&operand2=0') + .expect(200) + .end(function (err, res) { + expect(res.body).to.eql({ result: null }); + done(); + }); + }); + }); + describe('Multiplication', function () { it('multiplies two positive integers', function (done) { request.get('/arithmetic?operation=multiply&operand1=21&operand2=2')