From 774e016666d623f03fc99adbdf4af14f47cb7837 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 29 Apr 2022 22:50:33 +0530 Subject: [PATCH 1/3] Divide by zero error resolved --- src/logic/operate.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/logic/operate.js b/src/logic/operate.js index 0274e3a74..a763ec90c 100644 --- a/src/logic/operate.js +++ b/src/logic/operate.js @@ -13,7 +13,7 @@ export default function operate(numberOne, numberTwo, operation) { return one.times(two).toString(); } if (operation === "÷") { - if (two === "0") { + if (two.cmp(0) == 0) { //Used cmp function to compare the value alert("Divide by 0 error"); return "0"; } else { From 62a15b3728a335848a6899a19636cb7523844761 Mon Sep 17 00:00:00 2001 From: unknown Date: Sat, 30 Apr 2022 13:48:48 +0530 Subject: [PATCH 2/3] Keyboard input functionality added --- src/component/App.js | 36 ++++++++++++++++++++++++++++++++++-- src/component/ButtonPanel.js | 1 + 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/component/App.js b/src/component/App.js index b2b4fec53..70181e3d0 100644 --- a/src/component/App.js +++ b/src/component/App.js @@ -3,6 +3,8 @@ import Display from "./Display"; import ButtonPanel from "./ButtonPanel"; import calculate from "../logic/calculate"; import "./App.css"; +import useKeypress from "react-use-keypress"; + export default class App extends React.Component { state = { @@ -11,14 +13,44 @@ export default class App extends React.Component { operation: null, }; + constructor(props){ + super(props); + this.inputRef = React.createRef(); //ref to focus master div + } + + componentDidMount(){ + this.inputRef.current.focus(); //focusing master div to listen keyboard press + } + + keyHandler = (e) => { + console.log(e.key); + if(e.key == 0 || e.key == 1 || e.key == 2 || e.key == 3 || e.key == 4 || e.key == 5 || e.key == 6 || e.key == 7 || e.key == 7 || e.key == 8 || e.key == 9 + || e.key == '+' || e.key == '-' || e.key == "%" || e.key == "."){ + this.setState(calculate(this.state, e.key)); + } + else if(e.key == "Enter"){ + this.setState(calculate(this.state, "=")); + } + else if(e.key == "*"){ + this.setState(calculate(this.state, 'x')); + } + else if(e.key == "/"){ + this.setState(calculate(this.state, '÷')); + } + + } + handleClick = buttonName => { + console.log(buttonName); this.setState(calculate(this.state, buttonName)); }; + + render() { return ( -
- +
+
); diff --git a/src/component/ButtonPanel.js b/src/component/ButtonPanel.js index 994bca43d..c02988e32 100644 --- a/src/component/ButtonPanel.js +++ b/src/component/ButtonPanel.js @@ -13,6 +13,7 @@ export default class ButtonPanel extends React.Component { this.props.clickHandler(buttonName); }; + render() { return (
From b98cb420ab1f1c8ae4876bdd4e25985800f5aaf4 Mon Sep 17 00:00:00 2001 From: unknown Date: Sat, 30 Apr 2022 13:53:11 +0530 Subject: [PATCH 3/3] Clean up the code --- src/component/App.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/component/App.js b/src/component/App.js index 70181e3d0..bc9fc560e 100644 --- a/src/component/App.js +++ b/src/component/App.js @@ -3,7 +3,6 @@ import Display from "./Display"; import ButtonPanel from "./ButtonPanel"; import calculate from "../logic/calculate"; import "./App.css"; -import useKeypress from "react-use-keypress"; export default class App extends React.Component { @@ -41,7 +40,6 @@ export default class App extends React.Component { } handleClick = buttonName => { - console.log(buttonName); this.setState(calculate(this.state, buttonName)); }; @@ -50,7 +48,7 @@ export default class App extends React.Component { render() { return (
- +
);