diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json
index ffdd4d3..c1fd916 100644
--- a/.devcontainer/devcontainer.json
+++ b/.devcontainer/devcontainer.json
@@ -12,10 +12,19 @@
"extensions": [
"GitHub.copilot",
"GitHub.copilot-labs"
- ]
+ ],
+ "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"
+ },
+
// Features to add to the dev container. More info: https://containers.dev/features.
// "features": {},
@@ -23,7 +32,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": {},
diff --git a/api/controller.js b/api/controller.js
index 949731c..4a88319 100644
--- a/api/controller.js
+++ b/api/controller.js
@@ -16,6 +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) },
+ //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 1c60f86..12ea7c0 100644
--- a/public/client.js
+++ b/public/client.js
@@ -33,6 +33,12 @@ function calculate(operand1, operand2, operation) {
case '/':
uri += "?operation=divide";
break;
+ case '^':
+ uri += "?operation=power";
+ break;
+ case 'sqrt':
+ uri += "?operation=sqrt";
+ 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 @@
+
+
diff --git a/test/arithmetic.test.js b/test/arithmetic.test.js
index deded48..a3943b9 100644
--- a/test/arithmetic.test.js
+++ b/test/arithmetic.test.js
@@ -94,7 +94,265 @@ 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();
+ });
+ });
+ });
+
+// 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();
+ });
+ });
+ });
+
+ // 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) {