Skip to content

Commit

Permalink
ISSUE andrewagain#16 bug-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Tara Black committed Nov 15, 2021
1 parent 37b5607 commit 8003a23
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 10 deletions.
4 changes: 4 additions & 0 deletions src/component/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ export default class App extends React.Component {
total: null,
next: null,
operation: null,
higherOrder: {
next: null,
operation: null,
},
};

handleClick = buttonName => {
Expand Down
93 changes: 88 additions & 5 deletions src/logic/calculate.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,25 @@ export default function calculate(obj, buttonName) {
total: null,
next: null,
operation: null,
// clear high order values
higherOrder: {},
};
}

if (isNumber(buttonName)) {
if (buttonName === "0" && obj.next === "0") {
return {};
}
// If there is a higher order operation
if (obj.higherOrder && obj.higherOrder.operation) {
return {
higherOrder: {
// set higher order number
next: buttonName,
operation: obj.higherOrder.operation,
},
};
}
// If there is an operation, update next
if (obj.operation) {
if (obj.next) {
Expand Down Expand Up @@ -111,11 +123,75 @@ export default function calculate(obj, buttonName) {

// User pressed an operation button and there is an existing operation
if (obj.operation) {
return {
total: operate(obj.total, obj.next, obj.operation),
next: null,
operation: buttonName,
};
// ISSUE #16 - Calculator makes the sum of the two previous numbers before multiplying
// If there is a higher order value, perform calculation
if (obj.higherOrder && obj.higherOrder.next) {
// calculate higher operation with next values
const higherOrderTotal = operate(
obj.next,
obj.higherOrder.next,
obj.higherOrder.operation,
);
if (isHigherOrderOperation(buttonName)) {
// Next is higher order operation
return {
// Don't calculate total yet
next: higherOrderTotal,
higherOrder: {
next: null,
// set higher order operation
operation: buttonName,
},
};
} else {
// Next is lower order operation
return {
// Calculate total
total: operate(obj.total, higherOrderTotal, obj.operation),
next: null,
// set lower order operation
operation: buttonName,
higherOrder: {
next: null,
operation: null,
},
};
}
}
// If operation is a higher order operation, perform calculation
if (isHigherOrderOperation(obj.operation)) {
return {
// perform total calculation
total: operate(obj.total, obj.next, obj.operation),
next: null,
// set operation
operation: buttonName,
};
}
// If next operation is a lower order operation
if (!isHigherOrderOperation(buttonName)) {
return {
// perform total calculation
total: operate(obj.total, obj.next, obj.operation),
next: null,
// set operation
operation: buttonName,
higherOrder: {
next: null,
operation: null,
},
};
}
// If next operation is a higher order operation
if (isHigherOrderOperation(buttonName)) {
return {
higherOrder: {
next: null,
// set operation
operation: buttonName,
},
};
}
}

// no operation yet, but the user typed one
Expand All @@ -132,3 +208,10 @@ export default function calculate(obj, buttonName) {
operation: buttonName,
};
}

// Did user press a higher order operation button
function isHigherOrderOperation(operation) {
if (operation === "x" || operation === "÷") {
return true;
}
}
27 changes: 22 additions & 5 deletions src/logic/calculate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,29 @@ describe("calculate", function() {
test(["+", "2", "+"], {
total: "2",
operation: "+",
higherOrder: {
next: null,
operation: null,
},
});

test(["+", "2", "+", "+"], {
total: "2",
operation: "+",
higherOrder: {
next: null,
operation: null,
},
});

test(["+", "2", "+", "5"], {
next: "5",
total: "2",
operation: "+",
higherOrder: {
next: null,
operation: null,
},
});

test(["+", "2", "5"], {
Expand All @@ -112,6 +124,10 @@ describe("calculate", function() {

test(["+", "6", "+", "5", "="], {
total: "11",
higherOrder: {
next: null,
operation: null,
},
});

test(["0", ".", "4"], {
Expand All @@ -133,8 +149,8 @@ describe("calculate", function() {
});

// should clear the operator when AC is pressed
test(["1", "+", "2", "AC"], {});
test(["+", "2", "AC"], {});
test(["1", "+", "2", "AC"], { higherOrder: {} });
test(["+", "2", "AC"], { higherOrder: {} });

test(["4", "%"], {
next: "0.04",
Expand All @@ -158,16 +174,17 @@ describe("calculate", function() {
//Test that pressing the multiplication or division sign multiple times should not affect the current computation
test(["2", "x", "x"], {
total: "2",
operation: "x"
operation: "x",
});

test(["2", "÷", "÷"], {
total: "2",
operation: "÷"
operation: "÷",
});

test(["2", "÷", "x", "+", "-", "x"], {
total: "2",
operation: 'x'
operation: "-",
higherOrder: { next: null, operation: "x" },
});
});

0 comments on commit 8003a23

Please sign in to comment.