Step 7
20 min
Store the first number and the operator, then get ready to enter the second.
Definition
An operator is a symbol that indicates the operation to perform: +, −, × or ÷. The numbers it acts on are called operands . In 12 × 3, the symbol × is the operator, and 12 and 3 are the two operands.
When you press ×, the calculator computes nothing: it memorizes the first number and the operator, then prepares the display for the second number.
This may seem surprising: you would expect an operation key to "do" something. But think about how we calculate by hand: we cannot multiply until we know the second number. The operator is therefore only a promise of an operation, which must be kept somewhere while waiting for what comes next. That "somewhere" is the state object.
A single small function is enough. It does not touch the display: as always, it simply modifies the state.
function chooseOperator(operator) {
state.previous = state.current;
state.operator = operator;
state.overwrite = true;
state.expression = "";
}previous = current: we set the first number aside. The current number stays shown on the display, but it is now "saved" in previous, ready to be used for the calculation.operator: we remember the operation to perform. The received value comes from the data-operator attribute of the clicked key.overwrite = true: the next digit typed will replace the display. Without this line, typing 3 after 12 × would give 123.expression = "": we clear the previous calculation from the top line, which no longer has any reason to be displayed as soon as a new calculation starts.Here is the state before and after pressing ×, when the display shows 12:
current is "12", previous and operator are empty, overwrite is false.current is still "12", previous is "12", operator is "*", overwrite is true.overwrite is true, appendDigit replaces current with that digit and sets overwrite back to false. The second number is then built normally, while previous keeps the first.This sequence shows the value of the overwrite flag: a single variable lets appendDigit and chooseOperator cooperate without knowing about each other.
For now, nobody calls this function: no key knows about it. Replace the click listener from the previous step with this version, which also reads operator:
keys.addEventListener("click", (event) => {
const button = event.target.closest("button");
if (!button) return;
const { digit, operator, action } = button.dataset;
if (digit !== undefined) {
appendDigit(digit);
} else if (operator) {
chooseOperator(operator);
} else if (action && actions[action]) {
actions[action]();
}
updateDisplay();
});operator in addition to digit and action from button.dataset.12 ×: updateDisplay() takes care of it, thanks to state.operator. No extra rendering line is needed: that is the benefit of having a single function that draws the display from the state.data-digit, a data-operator or a data-action: never several at once. Each branch of the if / else if therefore corresponds to a family of keys.digit !== undefined for digits (because "0" must not be confused with a missing value), but simply operator for operators: their value is never an empty string.What happens if the user types 12, ×, then changes their mind and clicks +? After the first press, current is still "12": the function therefore copies 12 into previous again (nothing changes) and replaces the operator. The result is exactly what you would expect: the top line goes from 12 × to 12 +, with no special code for this case.
One limitation to be aware of for now: if the user types a second number and then directly presses another operator, the function overwrites previous with that second number, without performing the first calculation. Since the calculation is not programmed yet, this behavior is normal at this stage.

The calculation is starting to take shape.
overwrite = true : the second number would stick to the first (123 instead of 3).chooseOperator : the function must only touch the state; the display remains the job of updateDisplay().data-operator in the HTML : if the operation keys do not have this attribute, operator is undefined and nothing happens on click.overwrite flag prepares the display for the second number .digit, operator or action depending on the key, then always redraws the display.Checkpoint
Type 12, then ×: the top line shows 12 ×. Type 3: the bottom shows 3 and the top stays 12 ×. Click + instead of ×: the operator is simply replaced. The calculation is not performed yet; that will be the role of =.
If nothing happens when you click an operator, open the console with F12 to read the error message, and check that the operation keys do carry a data-operator attribute in the HTML.