Skip to content

Commit

Permalink
refactor(component): evaluate square root
Browse files Browse the repository at this point in the history
  • Loading branch information
lemredd committed Jan 6, 2023
1 parent 54a11cf commit ce73b9b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
21 changes: 8 additions & 13 deletions src/components/CalculatorContainer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -145,22 +145,11 @@ function setOperationValue(newOperation: Operations) {
operation.value = newOperation
}
function evaluateExpression(evaluationMethod: Evaluations) {
mustClearEntryOnNextAppend.value = true
switch (evaluationMethod) {
case "": {
previousEntry.value = Number(entry.value)
const sqrt = Math.sqrt(Number(entry.value))
entry.value = String(sqrt)
break
}
}
}
function retrieveEvaluationResults(newEvaluation: Evaluations, result: number) {
mustClearEntryOnNextAppend.value = true
const mustSaveCurrentEntry = newEvaluation === "1/x"
|| newEvaluation === ""
|| newEvaluation === ""
if (!previousExpressionEvaluated.value) previousExpressionEvaluated.value = expressionToEvaluate.value
if (hasSavedPreviousResult.value) expressionToEvaluate.value = `${previousResult.value}${operation.value}${rightEntry.value}`
Expand Down Expand Up @@ -217,7 +206,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"
/>
<OperationalButton value="÷" @append-to-screen="setOperationValue" />
</div>
<div class="row">
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 @@ -65,6 +65,11 @@ function evaluateExpression() {
return Number(entry) * Number(entry)
}
function evaluateSquareRoot() {
const { entry } = props
return Math.sqrt(Number(entry))
}
switch(props.value) {
case "=": {
emit("emitEvaluationResult", props.value, evaluateWithEquals())
Expand All @@ -83,6 +88,7 @@ function evaluateExpression() {
break
}
case "": {
emit("emitEvaluationResult", props.value, evaluateSquareRoot())
break
}
}
Expand Down

0 comments on commit ce73b9b

Please sign in to comment.