Skip to content

Commit f05d4df

Browse files
committed
power function added
1 parent ec14e9d commit f05d4df

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

api/controller.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ exports.calculate = function(req, res) {
1616
'subtract': function(a, b) { return a - b },
1717
'multiply': function(a, b) { return a * b },
1818
'divide': function(a, b) { return a / b },
19+
'pow': function(a, b) { return Math.pow(a, b) }
1920
};
2021

2122
if (!req.query.operation) {

public/client.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ function calculate(operand1, operand2, operation) {
3333
case '/':
3434
uri += "?operation=divide";
3535
break;
36+
case '^':
37+
uri += "?operation=pow";
38+
break;
3639
default:
3740
setError();
3841
return;

public/index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
<button class="btn" onClick="equalPressed()">=</button>
4242

4343
<!-- TODO: Buttons -->
44+
<!-- add a button for a power (or exponential) function -->
45+
<button class="btn" onClick="operationPressed('^')">^</button>
4446
</div>
4547
</div>
4648
</div>

test/arithmetic.test.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,58 @@ describe('Arithmetic', function () {
9494
});
9595

9696
// TODO: Challenge #1
97+
98+
// add tests for subtraction, multiplication, division, and exponentiation
99+
describe('Subtraction', function () {
100+
it('subtracts two positive integers', function (done) {
101+
request.get('/arithmetic?operation=subtract&operand1=21&operand2=21')
102+
.expect(200)
103+
.end(function (err, res) {
104+
expect(res.body).to.eql({ result: 0 });
105+
done();
106+
});
107+
});
108+
it('subtracts zero from an integer', function (done) {
109+
request.get('/arithmetic?operation=subtract&operand1=42&operand2=0')
110+
.expect(200)
111+
.end(function (err, res) {
112+
expect(res.body).to.eql({ result: 42 });
113+
done();
114+
});
115+
});
116+
it('subtracts a negative integer from a positive integer', function (done) {
117+
request.get('/arithmetic?operation=subtract&operand1=21&operand2=-42')
118+
.expect(200)
119+
.end(function (err, res) {
120+
expect(res.body).to.eql({ result: 63 });
121+
done();
122+
});
123+
});
124+
it('subtracts two negative integers', function (done) {
125+
request.get('/arithmetic?operation=subtract&operand1=-21&operand2=-21')
126+
.expect(200)
127+
.end(function (err, res) {
128+
expect(res.body).to.eql({ result: 0 });
129+
done();
130+
});
131+
});
132+
it('subtracts an integer from a floating point number', function (done) {
133+
request.get('/arithmetic?operation=subtract&operand1=2.5&operand2=-5')
134+
.expect(200)
135+
.end(function (err, res) {
136+
expect(res.body).to.eql({ result: 7.5 });
137+
done();
138+
});
139+
});
140+
it('subtracts with negative exponent', function (done) {
141+
request.get('/arithmetic?operation=subtract&operand1=1.2e-5&operand2=-1.2e-5')
142+
.expect(200)
143+
.end(function (err, res) {
144+
expect(res.body).to.eql({ result: 2.4e-5 });
145+
done();
146+
});
147+
});
148+
}
97149

98150

99151
describe('Multiplication', function () {

0 commit comments

Comments
 (0)