Skip to content

Instantly share code, notes, and snippets.

@KMJ-007
Created December 23, 2021 07:07
Show Gist options
  • Select an option

  • Save KMJ-007/5d4dcac4f3e72e115147c0ba49f7cc41 to your computer and use it in GitHub Desktop.

Select an option

Save KMJ-007/5d4dcac4f3e72e115147c0ba49f7cc41 to your computer and use it in GitHub Desktop.
let s = this.state.value;
console.log("value of S",s);
//for not allowing conscutive decimal in row
if (e.target.value == "." && s.charAt(s.length - 1) == ".") {
this.setState({
value: s,
});
} //end of if
//this is for making last oprator in calculation (test 13)
else if( e.target.value == "+" ||
e.target.value == "*" ||
e.target.value == "/"){
//5*-5=-25
//5*-+5=10
//5*-+5=5*+5=25
// console.log(s)
if(s.charAt(s.length-1)=="+"||s.charAt(s.length-1)=="-"||s.charAt(s.length-1)=="*"||s.charAt(s.length-1)=="/"){
s=s.replace(s.charAt(s.length-1),"");
if(s.charAt(s.length-1)=="+"||s.charAt(s.length-1)=="-"||s.charAt(s.length-1)=="*"||s.charAt(s.length-1)=="/"){
s=s.replace(s.charAt(s.length-1),"");
}
}
s+=e.target.value
console.log(s,e.target.value)
this.setState({
value:s
})
}
//this for if user press ac then state value will be 0 so when user press any other number it should be replaced
else {
if(this.state.value=="0"){
this.setState({
value:e.target.value
})
}
//if user press = and after that if he press any number it should start new calculation(taking the number pressed in the state value)
else if((e.target.value=="0"||e.target.value=="1"||e.target.value=="2"||e.target.value=="3"||e.target.value=="4"||e.target.value=="5"||e.target.value=="6"||e.target.value=="7"||e.target.value=="8"||e.target.value=="9") && calculated==true){
//calculated is boolean variable if = is presed the valye will be true
calculated=false;
// console.log("yes i am number pressed after calculation completed")
// console.log("key i preseed",e.target.value);
this.setState({
value:e.target.value
})
}//if opreator is pressed after evaluation it should continue in the on going string
//propblem is here whenver this execute s.charAt is not function shows
else if( (e.target.value == "+" ||
e.target.value == "-" ||
e.target.value == "*" ||
e.target.value == "/") && calculated==true){
calculated==false;
// console.log("you have pressed opreator")
this.setState({
value:this.state.value+e.target.value
})
}
//if all above condition is wrong than it should add to the string
else{
this.setState({
value: this.state.value + e.target.value,
});
}
} //end of else
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment