t-eckert / react-rpn

A Reverse Polish Notation Calculator in ReactJS
2 stars 0 forks source link

There's no limits to the period key #4

Closed AustinTSchaffer closed 6 years ago

AustinTSchaffer commented 6 years ago

There's no limits to the period key. Also, the values with extraneous periods will be parsed by the calculator. It almost looks like the values will be parsed up until the second period. In the example image, the result of adding 2....etc...0 and 1.2..3...etc is 3.2.

It seems like there needs to be an additional check on the user's input that takes the current state into consideration, such as a count of the dot characters or a boolean flag that keeps track of the "dot state" of the current value.

image

t-eckert commented 6 years ago

Added a check to make sure only one decimal point allowed.

decimalToInputActive = () => {
  const stackVals = this.state.stack
  let inputActive = stackVals[0]
  inputActive = inputActive.toString()
  if (inputActive.includes('.')) {
    console.log("A . is already in the display.")
  }
  else {
     inputActive = inputActive +'.'
     this.setState({stack: [inputActive,...stackVals.slice(1)]})
     console.log(inputActive)
   } 
}