Skip to content

Commit

Permalink
% Operation
Browse files Browse the repository at this point in the history
  • Loading branch information
hnaa0 committed Jul 28, 2023
1 parent 919553d commit bead60b
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ function App() {
};

const getResult = () => {
let updateResult = eval(calcState.calcForm.replaceAll("×", "*"));
let updateResult = eval(
calcState.calcForm.replaceAll("×", "*").replaceAll("%", "*0.01")
);
if (Number.isInteger(updateResult)) {
setCalcState({ ...calcState, calcResult: updateResult });
} else {
Expand Down Expand Up @@ -72,6 +74,17 @@ function App() {
setCalcState({ calcForm: updateform });
};

const appendPercent = (value) => {
if (
(calcState.calcForm === "") |
isNaN(calcState.calcForm.charAt(calcState.calcForm.length - 1))
) {
return;
}
let updateform = calcState.calcForm + value;
setCalcState({ calcForm: updateform });
};

const onClick = (value) => {
if (value === "AC") {
allClear(value);
Expand All @@ -88,8 +101,15 @@ function App() {
appendOperator(value);
} else if ((value === ".") & !calcState.calcForm.includes(".")) {
appendPoint(value);
} else if (value === "%") {
appendPercent(value);
} else if (value === "=") {
getResult();
if (
isNumber(calcState.calcForm.charAt(calcState.calcForm.length - 1)) |
(calcState.calcForm.charAt(calcState.calcForm.length - 1) === "%")
) {
getResult();
}
}
};

Expand Down

0 comments on commit bead60b

Please sign in to comment.