a,b calculations now working, but not a,b,c
This commit is contained in:
parent
6207015a7a
commit
bb48e5cf6d
|
@ -1,6 +1,6 @@
|
||||||
export default function Button(props){
|
export default function Button(props){
|
||||||
|
|
||||||
return(
|
return(
|
||||||
<button onClick={props.handleInput}>{props.children}</button>
|
<button className="button" id={props.id} onClick={props.handleInput}>{props.children}</button>
|
||||||
)
|
)
|
||||||
}
|
}
|
|
@ -2,12 +2,20 @@ import { useState } from "react";
|
||||||
import KeyPad from "./KeyPad";
|
import KeyPad from "./KeyPad";
|
||||||
|
|
||||||
export default function Calculator(props){
|
export default function Calculator(props){
|
||||||
const [main,setMain] = useState(0)
|
const [main,setMain] = useState("0")
|
||||||
const [memory,setMemory] = useState(0)
|
const [memory,setMemory] = useState("0")
|
||||||
const [operation,setOperation] = useState("")
|
const [operation,setOperation] = useState("")
|
||||||
|
|
||||||
const map = {
|
const map = {
|
||||||
conc:(numberString)=>{setMain(prev=>prev+numberString)},
|
conc:(numberString)=>{
|
||||||
|
if(main[0]==="0"){
|
||||||
|
console.log("STARTS WITH ZERO")
|
||||||
|
setMain(prev=>prev.slice(1)+numberString)
|
||||||
|
}else{
|
||||||
|
setMain(prev=>prev+numberString)
|
||||||
|
}
|
||||||
|
|
||||||
|
},
|
||||||
|
|
||||||
zero:function(){this.conc("0")},
|
zero:function(){this.conc("0")},
|
||||||
one:function(){this.conc("1")},
|
one:function(){this.conc("1")},
|
||||||
|
@ -20,7 +28,7 @@ export default function Calculator(props){
|
||||||
eight:function(){this.conc("8")},
|
eight:function(){this.conc("8")},
|
||||||
nine:function(){this.conc("9")},
|
nine:function(){this.conc("9")},
|
||||||
decimal:function(){
|
decimal:function(){
|
||||||
if(!/./g.test(main)){
|
if(!/\./g.test(main)){
|
||||||
this.conc(".")
|
this.conc(".")
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -40,9 +48,20 @@ export default function Calculator(props){
|
||||||
add:(a,b)=>{return a+b},
|
add:(a,b)=>{return a+b},
|
||||||
subtract:(a,b)=>{return a-b},
|
subtract:(a,b)=>{return a-b},
|
||||||
multiply:(a,b)=>{return a*b},
|
multiply:(a,b)=>{return a*b},
|
||||||
divide:(a,b)=>{return a/b},
|
divide:(a,b)=>{return b/a},
|
||||||
},
|
},
|
||||||
equals:()=>{setMain(prev=>{this.operations[operation](prev,memory)})}
|
|
||||||
|
clear:()=>{
|
||||||
|
setMain("0")
|
||||||
|
setMemory("0")
|
||||||
|
setOperation("")
|
||||||
|
},
|
||||||
|
equals:function(){
|
||||||
|
setMain(prev=>{
|
||||||
|
return this.operations[operation](parseInt(prev),parseInt(memory)).toString()
|
||||||
|
})
|
||||||
|
setOperation("")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleInput(content){
|
function handleInput(content){
|
||||||
|
@ -53,9 +72,9 @@ export default function Calculator(props){
|
||||||
|
|
||||||
return(
|
return(
|
||||||
<>
|
<>
|
||||||
<div id="display">
|
<div id="display-container">
|
||||||
<p id="memory">{memory}</p>
|
<p id="memory">{memory}</p>
|
||||||
<p id="main">{main}</p>
|
<p id="display">{main}</p>
|
||||||
<p id="operation">{operation}</p>
|
<p id="operation">{operation}</p>
|
||||||
</div>
|
</div>
|
||||||
<KeyPad handleInput={handleInput}/>
|
<KeyPad handleInput={handleInput}/>
|
||||||
|
|
|
@ -22,23 +22,37 @@ export default function NumPad(props){
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const functionMap = {
|
||||||
|
clear:"C",
|
||||||
|
equals:"="
|
||||||
|
}
|
||||||
|
|
||||||
|
function buildButtons(map){
|
||||||
const numPad = Object.keys(numberMap).map((e,i)=>{
|
return Object.keys(map).map((e,i)=>{
|
||||||
return(
|
return(
|
||||||
<Button
|
<Button
|
||||||
id={e}
|
id={e}
|
||||||
content={numberMap[e]}
|
content={map[e]}
|
||||||
key={"numPad"+e}
|
key={e+i}
|
||||||
handleInput={()=>props.handleInput(e)}>
|
handleInput={()=>props.handleInput(e)}>
|
||||||
{numberMap[e]}
|
{map[e]}
|
||||||
</Button>
|
</Button>
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
return(
|
return(
|
||||||
<div className="numPad">
|
<div className="keypad">
|
||||||
{numPad}
|
<div className="num-pad">
|
||||||
|
{buildButtons(numberMap)}
|
||||||
|
</div>
|
||||||
|
<div className="operator-pad">
|
||||||
|
{buildButtons(operatorMap)}
|
||||||
|
</div>
|
||||||
|
<div className="function-pad">
|
||||||
|
{buildButtons(functionMap)}
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
Loading…
Reference in New Issue