I'm working on a calculator project and I'm stuck at the point where I would like to string together several operations. Right now the calculator works but its functionality stops at one operation.
My idea is to store the result of the first operation as the num1 of a following operation, in case the user decides to proceed.
This is what the main function looks like right now:
let displayInput = "";
let num1 = "";
let num2 = "";
let operator = "";
buttons.forEach((button) => {
button.addEventListener("click", firstOperation);
});
function firstOperation(e) {
if (e.target.classList.contains("clear")) {
clearIt();
} else if (e.target.classList.contains("digit")) {
display.textContent += e.target.innerText;
displayInput = +display.textContent;
} else if (e.target.classList.contains("operator")) {
num1 = displayInput;
operator = e.target.innerText;
display.textContent = "";
} else if (e.target.classList.contains("equal")) {
if (num1 == "" && num2 == "" && operator == "") {
return;
} else {
num2 = displayInput;
display.textContent = "";
operate(operator, num1, num2);
let firstOp = +display.textContent;
}
}
}
I don't want a solution but if someone could point me in the right direction, that would be appreciated. Thanks.