Skip to content

Commit

Permalink
% fix percent sign behavior
Browse files Browse the repository at this point in the history
 - it was modulus but it should be divide by 100
  • Loading branch information
andrewagain committed May 27, 2018
1 parent 52308b3 commit bc78cfe
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
19 changes: 19 additions & 0 deletions src/logic/calculate.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Big from 'big.js';

import operate from './operate';
import isNumber from './isNumber';

Expand Down Expand Up @@ -43,6 +45,23 @@ export default function calculate(obj, buttonName) {
};
}

if (buttonName === '%') {
if (obj.operation && obj.next) {
const result = operate(obj.total, obj.next, obj.operation);
return {
total: Big(result).div(Big('100')).toString(),
next: null,
operation: null,
}
}
if (obj.next) {
return {
next: Big(obj.next).div(Big('100')).toString(),
}
}
return {};
}

if (buttonName === '.') {
if (obj.next) {
// ignore a . if the next number already has one
Expand Down
26 changes: 25 additions & 1 deletion src/logic/calculate.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import calculate from './calculate';
import { expect } from 'chai';
import chai from 'chai';

// https://github.com/chaijs/chai/issues/469
chai.config.truncateThreshold = 0

const expect = chai.expect;

function pressButtons(buttons) {
const value = {};
Expand Down Expand Up @@ -129,3 +134,22 @@ test(['.', '4', '-', '.', '2', '='], {
// should clear the operator when AC is pressed
test(['1', '+', '2', 'AC'], {});
test(['+', '2', 'AC'], {});

test(['4', '%'], {
next: '0.04',
});

test(['4', '%', 'x', '2', '='], {
total: '0.08',
});

test(['4', '%', 'x', '2'], {
total: '0.04',
operation: 'x',
next: '2',
});

// the percentage sign should also act as '='
test(['2', 'x', '2', '%'], {
total: '0.04',
});
3 changes: 0 additions & 3 deletions src/logic/operate.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,5 @@ export default function operate(numberOne, numberTwo, operation) {
if (operation === '÷') {
return one.div(two).toString();
}
if (operation === '%') {
return one.mod(two).toString();
}
throw Error(`Unknown operation '${operation}'`);
}

0 comments on commit bc78cfe

Please sign in to comment.