reactjs - Calc App React Undefined -
i creating calculator in react , have state value of displayvalue , keeps returning undefined when perform 1 of operationsmethod. unsure doing incorrectly if can appreciated thanks.
i thinking has performoperation method unsure what.
export default class app extends component { constructor(props) { super(props) this.state = {displayvalue: '0' , waitingforoperand: false, operator: null, value: null} this.inputdigit = this.inputdigit.bind(this); this.inputdot = this.inputdot.bind(this); this.clear = this.clear.bind(this); this.togglesign = this.togglesign.bind(this); this.performoperation = this.performoperation.bind(this); } clear() { const {displayvalue} = this.state; this.setstate({ displayvalue: '0' }); } inputdigit(digit) { const {displayvalue, waitingforoperand} = this.state; if(waitingforoperand) { this.setstate({ displayvalue: string(digit), waitingforoperand: false }); } else { this.setstate({ displayvalue: displayvalue === '0' ? string(digit) : displayvalue + digit }); } } inputdot() { const {displayvalue, waitingforoperand} = this.state; if(waitingforoperand) { this.setstate({ displayvalue: '.', waitingforoperand: false }); } else if(displayvalue.indexof('.') === -1) { this.setstate({ displayvalue: displayvalue + '.' }); } } togglesign() { const {displayvalue} = this.state; this.setstate({ displayvalue : displayvalue.charat(0) === '-' ? displayvalue.substring(1) : '-' + displayvalue }) } percent() { const {displayvalue} = this.state; const value = parsefloat(displayvalue); this.setstate({ displayvalue: string(value / 100) }); } performoperation(nextoperator) { const {displayvalue, operator, value} = this.state; const nextvalue = parsefloat(displayvalue); const operations = { '/' : (prevval, nextvalue) => prevval / nextvalue, '*' : (prevval, nextvalue) => prevval * nextvalue, '+' : (prevval, nextvalue) => prevval + nextvalue, '-' : (prevval, nextvalue) => prevval - nextvalue, '=' : (prevval, nextvalue) => nextvalue } if(value == null) { this.setstate({ value: nextvalue }); } else if(operator) { const currentvalue = value || 0; const computedvalue = operations[operator](currentvalue, computedvalue); this.setstate({ value: computedvalue, displayvalue: string(computedvalue) }) } this.setstate({ waitingforoperand: true, operator: nextoperator }) }
there problem line in performoperations
const computedvalue = operations[operator](currentvalue, computedvalue);
it should be:
const computedvalue = operations[operator](nextvalue, currentvalue);
anyway, variable names bit misleading.
Comments
Post a Comment