scopes fixed
This commit is contained in:
parent
c707e35a31
commit
6207015a7a
|
@ -1,6 +1,6 @@
|
||||||
export default function Button(props){
|
export default function Button(props){
|
||||||
|
|
||||||
return(
|
return(
|
||||||
<button>{props.children}</button>
|
<button onClick={props.handleInput}>{props.children}</button>
|
||||||
)
|
)
|
||||||
}
|
}
|
|
@ -1,6 +1,5 @@
|
||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
import NumPad from "./NumPad";
|
import KeyPad from "./KeyPad";
|
||||||
import Operators from "./Operators";
|
|
||||||
|
|
||||||
export default function Calculator(props){
|
export default function Calculator(props){
|
||||||
const [main,setMain] = useState(0)
|
const [main,setMain] = useState(0)
|
||||||
|
@ -10,26 +9,26 @@ export default function Calculator(props){
|
||||||
const map = {
|
const map = {
|
||||||
conc:(numberString)=>{setMain(prev=>prev+numberString)},
|
conc:(numberString)=>{setMain(prev=>prev+numberString)},
|
||||||
|
|
||||||
zero:()=>this.conc("0"),
|
zero:function(){this.conc("0")},
|
||||||
one:()=>this.conc("1"),
|
one:function(){this.conc("1")},
|
||||||
two:()=>this.conc("2"),
|
two:function(){this.conc("2")},
|
||||||
three:()=>this.conc("3"),
|
three:function(){this.conc("3")},
|
||||||
four:()=>this.conc("4"),
|
four:function(){this.conc("4")},
|
||||||
five:()=>this.conc("5"),
|
five:function(){this.conc("5")},
|
||||||
six:()=>this.conc("6"),
|
six:function(){this.conc("6")},
|
||||||
seven:()=>this.conc("7"),
|
seven:function(){this.conc("7")},
|
||||||
eight:()=>this.conc("8"),
|
eight:function(){this.conc("8")},
|
||||||
nine:()=>this.conc("9"),
|
nine:function(){this.conc("9")},
|
||||||
decimal:()=>{
|
decimal:function(){
|
||||||
if(!/./g.test(main)){
|
if(!/./g.test(main)){
|
||||||
this.conc(".")
|
this.conc(".")
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
add:()=>this.rcvOperator("add"),
|
add:function(){this.rcvOperator("add")},
|
||||||
subtract:()=>this.rcvOperator("subtract"),
|
subtract:function(){this.rcvOperator("subtract")},
|
||||||
multiply:()=>this.rcvOperator("multiply"),
|
multiply:function(){this.rcvOperator("multiply")},
|
||||||
divide:()=>this.rcvOperator("divide"),
|
divide:function(){this.rcvOperator("divide")},
|
||||||
|
|
||||||
rcvOperator:(operator)=>{
|
rcvOperator:(operator)=>{
|
||||||
setMemory(main)
|
setMemory(main)
|
||||||
|
@ -47,6 +46,7 @@ export default function Calculator(props){
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleInput(content){
|
function handleInput(content){
|
||||||
|
console.log("handling input!")
|
||||||
map[content]()
|
map[content]()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -58,8 +58,8 @@ export default function Calculator(props){
|
||||||
<p id="main">{main}</p>
|
<p id="main">{main}</p>
|
||||||
<p id="operation">{operation}</p>
|
<p id="operation">{operation}</p>
|
||||||
</div>
|
</div>
|
||||||
<NumPad handlemain={handleInput}/>
|
<KeyPad handleInput={handleInput}/>
|
||||||
<Operators handlemain={handleInput}/>
|
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
import Button from "./Button"
|
import Button from "./Button"
|
||||||
export default function NumPad(props){
|
export default function NumPad(props){
|
||||||
const map = {
|
const numberMap = {
|
||||||
zero:0,
|
zero:0,
|
||||||
one:1,
|
one:1,
|
||||||
two:2,
|
two:2,
|
||||||
|
@ -11,20 +11,27 @@ export default function NumPad(props){
|
||||||
seven:7,
|
seven:7,
|
||||||
eight:8,
|
eight:8,
|
||||||
nine:9,
|
nine:9,
|
||||||
|
decimal:"."
|
||||||
|
}
|
||||||
|
|
||||||
|
const operatorMap = {
|
||||||
add:"+",
|
add:"+",
|
||||||
subtract:"-",
|
subtract:"-",
|
||||||
multiply:"x",
|
multiply:"x",
|
||||||
divide:"/",
|
divide:"/",
|
||||||
decimal:"."
|
|
||||||
}
|
}
|
||||||
const numPad = Object.keys(map).map((e,i)=>{
|
|
||||||
|
|
||||||
|
|
||||||
|
const numPad = Object.keys(numberMap).map((e,i)=>{
|
||||||
return(
|
return(
|
||||||
<Button
|
<Button
|
||||||
id={e}
|
id={e}
|
||||||
content={map[e]}
|
content={numberMap[e]}
|
||||||
key={"numPad"+e}
|
key={"numPad"+e}
|
||||||
handleInput={props.handleInput}>
|
handleInput={()=>props.handleInput(e)}>
|
||||||
{i}
|
{numberMap[e]}
|
||||||
</Button>
|
</Button>
|
||||||
)
|
)
|
||||||
})
|
})
|
|
@ -1,23 +0,0 @@
|
||||||
import Button from "./Button";
|
|
||||||
export default function Operators(props){
|
|
||||||
const map = {
|
|
||||||
add:"+",
|
|
||||||
subtract:"-",
|
|
||||||
multiply:"x",
|
|
||||||
divide:"/",
|
|
||||||
decimal:"."
|
|
||||||
}
|
|
||||||
|
|
||||||
const operators = Object.keys(map).map(e=>{
|
|
||||||
return <Button
|
|
||||||
id={e}
|
|
||||||
content={map[e]}
|
|
||||||
handleInput={props.handleInput}
|
|
||||||
key={e}>
|
|
||||||
{map[e]}
|
|
||||||
</Button>
|
|
||||||
})
|
|
||||||
|
|
||||||
return ( <div className="operations-pad">{operators}</div>)
|
|
||||||
|
|
||||||
}
|
|
Loading…
Reference in New Issue