operations callback table in Calculator

This commit is contained in:
Andrzej Stepien 2023-07-04 17:01:52 +02:00
parent 113aa388a1
commit 38d008b050
1 changed files with 12 additions and 2 deletions

View File

@ -8,11 +8,21 @@ export default function Calculator(props){
const [operation,setOperation] = useState("")
function handleInput(content){
const operations = {
"+":(a,b)=>{return a+b},
"-":(a,b)=>{return a-b},
"X":(a,b)=>{return a*b},
"/":(a,b)=>{return a/b},
}
if(typeof content === "number" ){
setInput(prev=>{return prev.toString()+content.toString()})
}else{ //EXTRA CONDITION FOR DECIMAL, REGEX TO MAKE IT MAKE SENSE
setMemory(input)
setMemory(prev=>{operations[content](prev,input)})
setInput(0)
setOperation(content)
}