Skip to content

Instantly share code, notes, and snippets.

@PabloPenia
Last active September 28, 2022 17:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PabloPenia/c8e4ada15b2888ed664edd606a690ca8 to your computer and use it in GitHub Desktop.
Save PabloPenia/c8e4ada15b2888ed664edd606a690ca8 to your computer and use it in GitHub Desktop.

You shouldn't use this code

Why? this is an old file that was used to test some things while working on freecodecamp curriculum.

If you need some of these implementations, rather check the finished work at: React Calculator repository.

More info there.

import { useState } from 'react'
import Display from './Display'
import Pad from './Pad'
function App() {
const [currentVal, setCurrentVal] = useState('0')
const [formula, setFormula] = useState([])
const [calculating, setCalculating] = false
const [result, setResult] = useState('')
const isOp = /[x/+-]/
const make = {
clear: () => {
setFormula([])
setCurrentVal('0')
},
clearLast: () => {
if (currentVal.length === 1) {
if (formula.length === 0) {
return make.clear()
}
setCurrentVal(formula.at(-1).toString())
setFormula(formula.slice(0, -1))
} else {
setCurrentVal(currentVal.slice(0, -1))
}
},
decimal: () => {
if (currentVal.includes('.')) return
if (isOp.test(currentVal)) {
make.push()
return setCurrentVal('0.')
} else {
return setCurrentVal(currentVal + '.')
}
},
result: () => {
// fix last input before start calculating
setCalculating = true
make.push()
let tempResult
switch (formula.at(-1)) {
case '+':
case '-':
tempResult = [...formula, 0]
break
case 'x':
case '/':
tempResult = [...formula, 1]
break
default:
tempResult = [...formula]
}
// optimizing array before handling precedence:
// minus is already converted in number in every make.push
// deleting plus....
tempResult = tempResult.filter(item => item !== '+')
//Handling precedence and calculating
calculate(tempResult)
function calculate(arr) {
let index = arr.findIndex(item => isNaN(item))
if (index > 0) {
let prev = index - 1
let post = index + 1
let calc
arr[index] === 'x'
? (calc = prev * post)
: (calc = prev / post)
switch (index) {
case 1:
tempResult = [calc, ...tempResult.slice(3)]
calculate(tempResult)
break
case 2:
tempResult = [
tempResult[0],
calc,
...tempResult.slice(4),
]
calculate(tempResult)
break
default:
let reduced = tempResult
.slice(0, prev)
.reduce((a, b) => a + b)
tempResult = [
reduced,
calc,
...tempResult.slice(4),
]
calculate(tempResult)
}
} else {
console.log(tempResult)
}
}
console.log(
`Multiplicar: ${precalc.prod} length: ${precalc.prod.length}, Dividir: ${precalc.div} length: ${precalc.div.length}`
)
},
num: e => {
const value = e.target.value
if (currentVal === '0') return setCurrentVal(value)
if (isOp.test(currentVal)) {
make.push()
setCurrentVal(value)
} else {
setCurrentVal(currentVal + value)
}
},
op: e => {
const op = e.target.value
if (op === '-') {
if (currentVal === '-') {
return setCurrentVal('+')
} else {
make.push()
return setCurrentVal(op)
}
}
if (isOp.test(currentVal)) {
return setCurrentVal(op)
}
if (currentVal !== '0') {
make.push()
setCurrentVal(op)
}
},
push: () => {
let newFormula = [...formula]
let value = currentVal
if (!isOp.test(value)) {
value = parseFloat(parseFloat(value).toFixed(4))
if (formula.at(-1) === '-') {
value = -value
newFormula = [...formula.slice(0, -1)]
}
}
setFormula([...newFormula, value])
calculating && make.result()
return
},
}
return (
<main>
<Display current={currentVal} formula={formula} />
<Pad handler={make} />
</main>
)
}
export default App
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment