Skip to content

Commit

Permalink
keep the same value when user selects operator in calculator
Browse files Browse the repository at this point in the history
  • Loading branch information
emanuelefavero committed Mar 6, 2024
1 parent 6a1b9d5 commit bec195c
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions calculator.html
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,13 @@

<script>
// TODO shrink display text size when there are more than 6 numbers and so on (use display.innerText.length)
// TODO change the background color of the operator when it's selected

const display = document.getElementById('display')
const operatorButtons = document.querySelectorAll('.operator')
let firstValue = 0
let secondValue = 0
let operator = ''
let operatorSelected = false

function appendNumber(number) {
if (display.innerText === 'Error') {
Expand All @@ -129,6 +129,12 @@
display.innerText = ''
}

// Clear display if an operator is selected
if (operatorSelected) {
display.innerText = ''
operatorSelected = false // reset flag after clearing
}

display.innerText += number

resetOperatorColor()
Expand All @@ -137,7 +143,8 @@
function selectOperator(selectedOperator) {
firstValue = parseFloat(display.innerText)
operator = selectedOperator
display.innerText = '0'
// display.innerText = '0'
operatorSelected = true

changeOperatorColor()
}
Expand Down

0 comments on commit bec195c

Please sign in to comment.