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
37 changes: 10 additions & 27 deletions components/Calculator.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const Calculator = () => {

const handleChange = (e) => {
setOperation(e.target.value);

};

useEffect(() => {
Expand All @@ -33,14 +34,18 @@ const Calculator = () => {
e.preventDefault();
const query = {
operation: operation,
first: firstRef.current.value,
second: secondRef.current.value,
first: parseInt(firstRef.current.value),
second: parseInt(secondRef.current.value),
};




axios
.get(`/api/calculate/${query.operation}/${query.first}/${query.second}`)
.then((res) => {
setResult(res.data.result);

})
.catch((err) => {
console.log(err.response.data.message);
Expand All @@ -51,8 +56,8 @@ const Calculator = () => {
e.preventDefault();
setOperation("");
setResult(welcomeMessage);
firstRef.current.value = null;
secondRef.current.value = null;
firstRef.current.value = "";
secondRef.current.value = "";
document.activeElement.blur();
};

Expand Down Expand Up @@ -107,7 +112,7 @@ const Calculator = () => {
</Grid2>
<Grid2 xs={2}>
<FormControl fullWidth>
<Button variant="outlines" onChange={handleReset}>
<Button variant="outlines" onClick={handleReset}>
Reset
</Button>
</FormControl>
Expand All @@ -129,25 +134,3 @@ const Calculator = () => {
);
};
export default Calculator;






















11 changes: 8 additions & 3 deletions pages/api/calculate/[...params].js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { add, multiply, divide } from "../../../utils/calculate";
import { add, subtract, multiply, divide } from "../../../utils/calculate";

export default function handler(req, res) {
try {
Expand All @@ -12,12 +12,17 @@ export default function handler(req, res) {
let result;
switch (params.operation) {
case "add":
result = add(params.first, params.second);
const firstNumber = parseFloat(params.first);
const secondNumber = parseFloat(params.second);
if (isNaN(firstNumber) || isNaN(secondNumber)) {
throw new Error("Invalid numbers provided");
}
result = add(firstNumber, secondNumber);
break;
case "subtract":
result = subtract(params.first, params.second);
break;
case "Multiply":
case "multiply":
result = multiply(params.first, params.second);
break;
case "divide":
Expand Down
2 changes: 1 addition & 1 deletion utils/calculate.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const multiply = (first, second) => {
};

export const divide = (first, second) => {
return first % second;
return first / second;
};


Expand Down