Skip to content
This repository has been archived by the owner on Jan 18, 2023. It is now read-only.

Divide by zero error resolved #127

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
34 changes: 32 additions & 2 deletions src/component/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,51 @@ import ButtonPanel from "./ButtonPanel";
import calculate from "../logic/calculate";
import "./App.css";


export default class App extends React.Component {
state = {
total: null,
next: null,
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 => {
this.setState(calculate(this.state, buttonName));
};



render() {
return (
<div className="component-app">
<Display value={this.state.next || this.state.total || "0"} />
<div className="component-app" ref={this.inputRef} onKeyDown={this.keyHandler} tabIndex="0">
<Display value={this.state.next || this.state.total || "0"}/>
<ButtonPanel clickHandler={this.handleClick} />
</div>
);
Expand Down
1 change: 1 addition & 0 deletions src/component/ButtonPanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default class ButtonPanel extends React.Component {
this.props.clickHandler(buttonName);
};


render() {
return (
<div className="component-button-panel">
Expand Down
2 changes: 1 addition & 1 deletion src/logic/operate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down