Calculator setup.

main
sigonasr2, Sig, Sigo 3 years ago
parent 07e69074ec
commit bc85833521
  1. 42
      equations/src/App.js
  2. 6
      package-lock.json

@ -17,16 +17,47 @@ function Equation(p) {
}}> }}>
{Object.keys(data).map((key)=><option key={key} value={key}>{key}</option>)} {Object.keys(data).map((key)=><option key={key} value={key}>{key}</option>)}
</select><br/> </select><br/>
{data[field]}</div><div style={{display:"inline-block",border:"1px solid black"}}> {data[field]}</div>{operator!=="disabled"&&<div style={{display:"inline-block",border:"1px solid black"}}>
<select onChange={(ev)=>{updateEquation(field,ev.currentTarget.value)}}> <select onChange={(ev)=>{updateEquation(field,ev.currentTarget.value)}}>
{["+","-","x","÷","()"].map((key)=><option key={key} value={key}>{key}</option>)} {["+","-","×","÷","()"].map((key)=><option key={key} value={key}>{key}</option>)}
</select><br/>{id}</div></> </select><br/> </div>}</>
} }
function EquationBuilder(p) { function EquationBuilder(p) {
const {data} = p const {data} = p
const [equationParts,setEquationParts] = useState([]) const [equationParts,setEquationParts] = useState([])
const [equation,setEquation] = useState([]) const [equation,setEquation] = useState([])
function SolveEquation(eq) {
var newEq = [...eq]
//Multiplication and Division first.
for (var i=0;i<newEq.length-1;i++) {
if (newEq[i]==="×") {
var product = Number(newEq[i-1])*Number(newEq[i+1])
newEq.splice(i-1,3,product)
i--
} else
if (newEq[i]==="÷") {
var quotient = Number(newEq[i-1])/Number(newEq[i+1])
newEq.splice(i-1,3,quotient)
i--
}
}
for (var i=0;i<newEq.length-1;i++) {
if (newEq[i]==="+") {
var sum = Number(newEq[i-1])+Number(newEq[i+1])
newEq.splice(i-1,3,sum)
i--
} else
if (newEq[i]==="-") {
var difference = Number(newEq[i-1])-Number(newEq[i+1])
newEq.splice(i-1,3,difference)
i--
}
}
return newEq.slice(0,-1)
}
useEffect(()=>{ useEffect(()=>{
var newEq = [] var newEq = []
for (var i=0;i<equationParts.length;i++) { for (var i=0;i<equationParts.length;i++) {
@ -39,9 +70,10 @@ function EquationBuilder(p) {
<button onClick={()=>{ <button onClick={()=>{
setEquationParts([...equationParts,{field:Object.keys(data)[0],operator:"+"}])}}>+</button> setEquationParts([...equationParts,{field:Object.keys(data)[0],operator:"+"}])}}>+</button>
<br/><br/> <br/><br/>
{equationParts.map((eq,i)=><Equation equationParts={equationParts} setEquationParts={setEquationParts} id={i} data={data} field={equationParts[i].field} operator={equationParts[i].operator}/>)} {equationParts.map((eq,i)=><Equation key={i} equationParts={equationParts} setEquationParts={setEquationParts} id={i} data={data} field={equationParts[i].field} operator={i!==equationParts.length-1?equationParts[i].operator:"disabled"}/>)}
<br/><br/> <br/><br/>
{equation} {equation.slice(0,-1)}
<br/><h1>{SolveEquation(equation)}</h1>
</> </>
} }

6
package-lock.json generated

@ -0,0 +1,6 @@
{
"name": "EquationsBuilder",
"lockfileVersion": 2,
"requires": true,
"packages": {}
}
Loading…
Cancel
Save