Skip to content

Commit

Permalink
refactor(component): evaluate square
Browse files Browse the repository at this point in the history
  • Loading branch information
lemredd committed Jan 6, 2023
1 parent 48e2e96 commit 9fa28a8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
18 changes: 10 additions & 8 deletions src/components/CalculatorContainer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,6 @@ function setOperationValue(newOperation: Operations) {
function evaluateExpression(evaluationMethod: Evaluations) {
mustClearEntryOnNextAppend.value = true
switch (evaluationMethod) {
case "": {
previousEntry.value = Number(entry.value)
const sqr = Number(entry.value) * Number(entry.value)
entry.value = String(sqr)
break
}
case "": {
previousEntry.value = Number(entry.value)
const sqrt = Math.sqrt(Number(entry.value))
Expand All @@ -165,10 +159,12 @@ function evaluateExpression(evaluationMethod: Evaluations) {
function retrieveEvaluationResults(newEvaluation: Evaluations, result: number) {
mustClearEntryOnNextAppend.value = true
const mustSaveCurrentEntry = newEvaluation === "1/x"
|| newEvaluation === ""
if (!previousExpressionEvaluated.value) previousExpressionEvaluated.value = expressionToEvaluate.value
if (hasSavedPreviousResult.value) expressionToEvaluate.value = `${previousResult.value}${operation.value}${rightEntry.value}`
if (newEvaluation === "1/x") previousEntry.value = Number(entry.value)
if (mustSaveCurrentEntry) previousEntry.value = Number(entry.value)
evaluation.value = newEvaluation
previousResult.value = String(result)
Expand Down Expand Up @@ -214,7 +210,13 @@ function retrieveEvaluationResults(newEvaluation: Evaluations, result: number) {
:expression-and-previous-result-information="expressionAndPreviousResultInformation"
@emit-evaluation-result="retrieveEvaluationResults"
/>
<EvaluationButton value="" @append-to-screen="retrieveEvaluationResults" />
<EvaluationButton
value=""
:entry="entry"
:expression-to-evaluate="expressionToEvaluate"
:expression-and-previous-result-information="expressionAndPreviousResultInformation"
@emit-evaluation-result="retrieveEvaluationResults"
/>
<EvaluationButton value="" @append-to-screen="retrieveEvaluationResults" />
<OperationalButton value="÷" @append-to-screen="setOperationValue" />
</div>
Expand Down
6 changes: 6 additions & 0 deletions src/components/CalculatorContainer/EvaluationButton.vue
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ function evaluateExpression() {
return 1 / Number(entry)
}
function evaluateSquare() {
const { entry } = props
return Number(entry) * Number(entry)
}
switch(props.value) {
case "=": {
emit("emitEvaluationResult", props.value, evaluateWithEquals())
Expand All @@ -74,6 +79,7 @@ function evaluateExpression() {
break
}
case "": {
emit("emitEvaluationResult", props.value, evaluateSquare())
break
}
case "": {
Expand Down

0 comments on commit 9fa28a8

Please sign in to comment.