I'm currently working on developing a calculator, but I've hit a roadblock that I can't seem to overcome. The numbers aren't appearing in the calculator window when I click them.
I've been struggling with understanding query selectors and event listeners, so it's likely that I've made a mistake somewhere in my JavaScript code.
I've set up a query selector targeting the .window-content class and initialized the windowValue variable as an empty string. Additionally, I've implemented an Event Listener on the container housing the calculator to listen for clicks.
The problematic code snippet is displayed below:
const screen = document.querySelector(".window-content");
let windowValue = "";
document.querySelector(".container").addEventListener("click", function(e) {
const tgt = e.target;
if (tgt.tagName !== "button") return;
let buttonText = tgt.innerText;
if (buttonText == 'X') {
buttonText = '*';
windowValue += buttonText;
screen.value = windowValue;
} else if (buttonText == 'CA') {
windowValue = "";
screen.value = windowValue;
} else if (buttonText == '🡄') {
windowValue = windowValue.slice(0,-1);
screen.value = windowValue;
} else if (buttonText == '=') {
screen.value = operate(firstNum, secondNum, operator);
windowValue = screen.value;
firstNum = "";
operator = "";
secondNum = "";
} else if (buttonText.match(/[0-9]/)) {
windowValue += buttonText;
screen.value = windowValue;
} else {
firstNum = windowValue;
operator = buttonText;
windowValue = "";
windowValue += buttonText;
windowContent.innerText = windowValue;
}
})