@vipatron
import React from "react";
import ReactDOM from "react-dom";
import "./styles.css";
// a decimal after a decimal between two digit strings
// two decimals in a row
// two of the SAME operator in a row(summing up four repetitive regexes)
// multiply or divide if no digit has been entered since power on / clear
// two zeroes at the beginning of the string(doesn’t cover the case of “5 + 005”)
// two zeroes at the beginning of a generic digit string(using positive lookbehind when lookbehind isn’t supported on all javascript engines)
var regex = /(\d*\.\d+\.)|\.{2,}|\+{2,}|-{2,}|\*{2,}|^[*/]|^00|(?<=[/+\-*])0{2}/g;
// var regex = /a/g;
// tests for an operator
var operatorRegex = /[*\/\-\+]/;
// tests whether a operator is the last character in inputString
var operatorLast = /[*\/\-\+]$/;
class Calculator extends React.Component {
constructor(props) {
super(props);
this.state = {
inputString: ""
};
this.handleInput = this.handleInput.bind(this);
this.handleClear = this.handleClear.bind(this);
this.handleEquals = this.handleEquals.bind(this);
}
handleInput(event) {
// console.log(this.state.inputString)
// if (!regex.test(this.state.inputString)) { //If our 'patterns we don't want ' regex doesn't match anything
if (
operatorRegex.test(event.target.value) && //If event value passed in is an operator
operatorLast.test(this.state.inputString) //If the last value of our inputString is an operator
) {
this.setState({
inputString: this.state.inputString.replace(
//replace the old operator with thte new one that was clicked
operatorLast,
event.target.value
)
});
} else {
this.setState({
inputString: this.state.inputString + event.target.value
});
}
// }
}
/*
handleClear will change inputString to it's defined state of "" which is what the tests call for
Displying 0 in the view is taken care of via the ternary operTION in the display declaration
*/
handleClear(event) {
this.setState({
inputString: ""
});
}
handleEquals() {
this.setState({
inputString: eval(this.state.inputString)
});
}
// shouldComponentUpdate() {
// let regex = /(\d*\.\d*\.)|\.{2}|\+{2}|\-{2}|^[*\/]|^00|(?<=\b)0{2}/g;
// return !regex.test(this.state.inputString);
// }
render() {
return (
<div className="Calc">
<button id="equals" value="=" onClick={this.handleEquals}>
=
</button>
<button id="zero" onClick={this.handleInput} value="0">
0
</button>
<button id="one" onClick={this.handleInput} value="1">
1
</button>
<button id="two" onClick={this.handleInput} value="2">
2
</button>
<button id="three" onClick={this.handleInput} value="3">
3
</button>
<button id="four" onClick={this.handleInput} value="4">
4
</button>
<button id="five" onClick={this.handleInput} value="5">
5
</button>
<button id="six" onClick={this.handleInput} value="6">
6
</button>
<button id="seven" onClick={this.handleInput} value="7">
7
</button>
<button id="eight" onClick={this.handleInput} value="8">
8
</button>
<button id="nine" onClick={this.handleInput} value="9">
9
</button>
<button id="add" value="+" onClick={this.handleInput}>
+
</button>
<button id="subtract" value="-" onClick={this.handleInput}>
-
</button>
<button id="divide" value="/" onClick={this.handleInput}>
/
</button>
<button id="multiply" value="*" onClick={this.handleInput}>
*
</button>
<button id="decimal" value="." onClick={this.handleInput}>
.
</button>
<button id="clear" onClick={this.handleClear}>
AC
</button>
<div id="display">
{this.state.inputString === "" ? 0 : this.state.inputString}
</div>
</div>
);
}
}
const rootElement = document.getElementById("root");
ReactDOM.render(<Calculator />, rootElement);