so, recently i made a calculator with html, css and js ! to add it to my portfolio
**code for js for operator **
function useOperator(operator) {
const currentValue = Number(calculatorDisplay.textContent)
// Prevent multiple operators
if (operatorValue && awaitingNextValue) {
operatorValue = operator
return
}
// Assign firstValue if no value
if (!firstValue) {
firstValue = currentValue;
} else {
const calculation = calculate[operatorValue](firstValue, currentValue)
calculatorDisplay.textContent = calculation
firstValue = calculation
}
// Ready for next value, store operator
awaitingNextValue = true;
operatorValue = operator;
}
*** for add event listener ***
// Add Event Listeners for numbers, operators, decimal buttons
inputBtns.forEach((inputBtn) => {
if (inputBtn.classList.length === 0) {
inputBtn.addEventListener('click', () => sendNumberValue(inputBtn.value))
} else if (inputBtn.classList.contains('operator')) {
inputBtn.addEventListener('click', () => useOperator(inputBtn.value))
} else if (inputBtn.classList.contains('decimal')) {
inputBtn.addEventListener('click', () => addDecimal())
}
})
Top comments (0)