I have a masterFunction which should fire another function called reset when the AC button on my calculator is pressed (picked up via e.target.id = "AC").
However it's not firing, in fact no functions are firing when I press the AC button.
I've added a console log and can see e.target.id = "AC" yet it still won't fire.
Reduced code:
//Update display with button clicked
buttonGrid.addEventListener('click', e => {
console.log(e.target.id);
masterFunction(e);
});
//Fire relevant function depending on the button pressed etc
let masterFunction = (e) => {
if (e.target.id === "AC") {
reset(e);
}
};
// Reset the screen
let reset = (e) => {
secondNumberCounter = false;
operatorPressed = false;
firstNumber = 0;
secondNumber = 0;
buttonNumber = 0;
decimalCounter = 0;
displayArea.textContent = 0;
};
Can anyone see what I'm doing wrong?
Because your first if condition is eating up the clicks. Basic debugging will show your problem.
console.log('BEFORE THE IFS');
if (operatorPressed === false && firstNumberCounter === true && e.target.dataset.type != "nonNumberFunction") {
console.log('I AM HERE!!!!')
firstNumberPicker(e);
}
and you should stop the default action of the clicks
buttonGrid.addEventListener('click', e => {
masterFunction(e);
e.preventDefault();
});