Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions api/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 4 additions & 1 deletion public/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ function calculate(operand1, operand2, operation) {
case '/':
uri += "?operation=divide";
break;
case '^':
uri += "?operation=power";
break;
default:
setError();
return;
Expand Down Expand Up @@ -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();
Expand Down
5 changes: 5 additions & 0 deletions public/default.css
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 5 additions & 1 deletion public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@

<button class="btn" onClick="numberPressed(0)">0</button>
<button class="btn" onClick="decimalPressed()">.</button>
<!-- power / exponent button -->
<button class="btn" onClick="operationPressed('^')">^</button>
<button class="btn" onClick="operationPressed('+')">+</button>
<button class="btn" onClick="equalPressed()">=</button>
<button class="btn span-4" onClick="equalPressed()">=</button>



</div>
</div>
Expand Down
31 changes: 29 additions & 2 deletions test/arithmetic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down