From 51f386eee9e3bc83905282f576622970c6846986 Mon Sep 17 00:00:00 2001
From: Saad Khalid <91637722+stargazer4@users.noreply.github.com>
Date: Wed, 17 Apr 2024 17:45:30 +0000
Subject: [PATCH] added power functionality
---
api/controller.js | 1 +
public/client.js | 3 +++
public/index.html | 2 ++
test/arithmetic.test.js | 52 +++++++++++++++++++++++++++++++++++++++++
4 files changed, 58 insertions(+)
diff --git a/api/controller.js b/api/controller.js
index 949731c..a371de7 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 },
+ 'pow': function(a, b) { return Math.pow(a, b) }
};
if (!req.query.operation) {
diff --git a/public/client.js b/public/client.js
index 1c60f86..f702edd 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=pow";
+ break;
default:
setError();
return;
diff --git a/public/index.html b/public/index.html
index 400c454..a95a30d 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..2146b26 100644
--- a/test/arithmetic.test.js
+++ b/test/arithmetic.test.js
@@ -94,6 +94,58 @@ describe('Arithmetic', function () {
});
// TODO: Challenge #1
+// add tests for subtraction, multiplication, division, and exponentiation
+ describe('Subtraction', function () {
+ it('subtracts two positive 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 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 an integer from a floating point number', function (done) {
+ request.get('/arithmetic?operation=subtract&operand1=2.5&operand2=-5')
+ .expect(200)
+ .end(function (err, res) {
+ expect(res.body).to.eql({ result: 7.5 });
+ 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 () {