I'm working on a calculator in react js. When I add two numbers it gives me the right answer but after I click on the minus button it adds first Value to the result and gives me the addition result. I'm trying to figure out where the problem is but I can't. Here is the code of my operation function
import React, { Component } from 'react';
export default class Calculator extends Component {
constructor(props)
{
super(props)
{
this.state={
value:'0',
firstValue:0,
operation:'',
previousvalue:'',
nextvalue:'',
showingTemporaryResult:false
}
};
this.handleclick=this.handleclick.bind(this);
this.operation=this.operation.bind(this);
this.cleardisplay=this.cleardisplay.bind(this);
this.dot=this.dot.bind(this);
this.changesign=this.changesign.bind(this);
this.percent=this.percent.bind(this);
}
handleclick(digit)
{
if(this.state.showingTemporaryResult) {
console.log("TRUE CALLED");
this.setState({value:String(digit),showingTemporaryResult:false})
}
else {
console.log("ELSE CALLED");
this.setState({value:this.state.value==='0'?String(digit):this.state.value + digit});
}
// console.log('helo click fire',this.state.value);
}
cleardisplay()
{
this.setState({value:'0',
firstValue:0,
operation:'',
previousvalue:'',
nextvalue:'',
showingTemporaryResult:false});
}
dot()
{
if(this.state.value.indexOf('.')===-1)
{
this.setState({value:this.state.value+'.'})
}
}
changesign()
{
console.log("Event Change Sign")
if(this.state.value.charAt(0)==='-')
{
this.setState({value:this.state.value.substr(1)})
// console.log(' sign not change');
}
else{
this.setState({value:'-'+this.state.value})
console.log('lets change sign');
}
}
operation(operator)
{
if(operator==='+'||operator==='-'||operator==='*'||operator==='/')
{
console.log('operator coming',operator);
this.setState({operation:operator});
console.log('operator coming',this.state.operation);
if(this.state.operator === '+') {
let secondValue = parseInt(this.state.value);
let firstValue = parseInt(this.state.firstValue);
console.log('chcking first value in plus ',firstValue);
let sum = secondValue+firstValue;
this.setState({firstValue:sum,value:sum,showingTemporaryResult:true,operation:operator});
//console.log('somthing here ');
}
else if(this.state.operator==='-')
{
let secondValue = parseInt(this.state.value);
let firstValue = parseInt(this.state.firstValue);
console.log('chcking first value in minus ',firstValue);
let sum = secondValue-firstValue;
this.setState({firstValue:sum,value:sum,showingTemporaryResult:true});
}
else if (this.state.operation === '') {
let firstValue = parseInt(this.state.value);
this.setState({firstValue});
this.setState({value:'',operation:operator});
console.log('or here ');
}
}
else if(operator==='=')
{
if(this.state.operation === '+') {
let answer = parseInt(this.state.value) + parseInt(this.state.firstValue);
this.setState({value:answer});
}
else if(this.state.operation==='-'){
let answer = parseInt(this.state.value) - parseInt(this.state.firstValue);
this.setState({value:answer});
}
}
}
percent()
{
this.setState({value:this.state.value/100})
}
render() {
return (
<div className="cal">
<input type="num" name="res" value={this.state.value} disabled style={{height:'8vh',width:'47vh',backgroundColor:'black',color:'white',fontSize:'20px'}}/><br></br>
<input type="button" name="ac" onClick={() => this.cleardisplay()} value="Ac" style={{height:'5vh',width:'12vh',backgroundColor:'#ccced1',border:'1px solid black'}}/>
<input type="button" name="+/-" onClick={() => this.changesign()} value="+/-" style={{height:'5vh',width:'12vh',backgroundColor:'#ccced1',border:'1px solid black'}}/>
<input type="button" name="%" onClick={() => this.percent()}value="%" style={{height:'5vh',width:'12vh',backgroundColor:'#ccced1',border:'1px solid black'}}/>
<input type="button" name="/" onClick={() => this.operation('/')} value='/' style={{height:'5vh',width:'12vh',backgroundColor:'#ff9100',border:'1px solid black'}}/>
<br></br>
<input type="button" name="7" onClick={() => this.handleclick(7)} value="7" style={{height:'5vh',width:'12vh',backgroundColor:'#d2d4d8',border:'1px solid black'}}/>
<input type="button" name="8" onClick={() => this.handleclick(8)} value="8" style={{height:'5vh',width:'12vh',backgroundColor:'#d2d4d8',border:'1px solid black'}}/>
<input type="button" name="9" onClick={() => this.handleclick(9)} value="9" style={{height:'5vh',width:'12vh',backgroundColor:'#d2d4d8',border:'1px solid black'}}/>
<input type="button" name="*" onClick={() => this.operation('*')} value='*' style={{height:'5vh',width:'12vh',backgroundColor:'#ff9100',border:'1px solid black'}}/>
<br></br>
<input type="button" name="4" onClick={() => this.handleclick(4)} value="4" style={{height:'5vh',width:'12vh',backgroundColor:'#d2d4d8',border:'1px solid black'}}/>
<input type="button" name="5" onClick={() => this.handleclick(5)} value="5" style={{height:'5vh',width:'12vh',backgroundColor:'#d2d4d8',border:'1px solid black'}}/>
<input type="button" name="6" onClick={() => this.handleclick(6)}value="6" style={{height:'5vh',width:'12vh',backgroundColor:'#d2d4d8',border:'1px solid black'}}/>
<input type="button" name="-" onClick={() => this.operation('-')} value='-' style={{height:'5vh',width:'12vh',backgroundColor:'#ff9100',border:'1px solid black'}}/>
<br></br>
<input type="button" name="1" onClick={() => this.handleclick(1)} value="1" style={{height:'5vh',width:'12vh',backgroundColor:'#d2d4d8',border:'1px solid black'}}/>
<input type="button" name="2" onClick={() => this.handleclick(2)} value="2" style={{height:'5vh',width:'12vh',backgroundColor:'#d2d4d8',border:'1px solid black'}}/>
<input type="button" name="3" onClick={() => this.handleclick(3)} value="3" style={{height:'5vh',width:'12vh',backgroundColor:'#d2d4d8',border:'1px solid black'}}/>
<input type="button" name="+" onClick={() => this.operation('+')} value='+' style={{height:'5vh',width:'12vh',backgroundColor:'#ff9100',border:'1px solid black'}}/>
<br></br>
<input type="button" name="0" onClick={() => this.handleclick(0)} value="0" style={{height:'5vh',width:'24vh',backgroundColor:'#d2d4d8',border:'1px solid black'}}/>
<input type="button" name="." onClick={() => this.dot()} value="." style={{height:'5vh',width:'12vh',backgroundColor:'#d2d4d8',border:'1px solid black'}}/>
<input type="button" name="=" onClick={() => this.operation('=')} value="=" style={{height:'5vh',width:'12vh',backgroundColor:'#ff9100',border:'1px solid black'}}/>
</div>
)
}
}
now i add my whole component in post .........................................................................................................................................................................................................................................