Step 7 / 1164%

Step 7

Choosing an Operator

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.

1. Memorizing the first number and the operator

A single small function is enough. It does not touch the display: as always, it simply modifies the state.

calculator.js (before updateDisplay();)
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:

  • Before : current is "12", previous and operator are empty, overwrite is false.
  • After : current is still "12", previous is "12", operator is "*", overwrite is true.
  • On the next digit : since 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.

2. Wiring up the operators

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:

calculator.js (replaces the click listener)
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();
});
  • We now read operator in addition to digit and action from button.dataset.
  • The top line already displays 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.
  • The order of the tests matters. A key has either a 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.
  • We test 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.

3. Changing your mind about the operator

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.

French formatting: thousands separator space and decimal comma.

The calculation is starting to take shape.

Common mistakes to avoid

  • Forgetting overwrite = true : the second number would stick to the first (123 instead of 3).
  • Adding the listener instead of replacing the old one : you would then have two listeners on the same container, and every click would be handled twice.
  • Writing to the display from chooseOperator : the function must only touch the state; the display remains the job of updateDisplay().
  • Forgetting data-operator in the HTML : if the operation keys do not have this attribute, operator is undefined and nothing happens on click.

Key takeaways

  • An operator computes nothing: it memorizes the first number and the operation.
  • The overwrite flag prepares the display for the second number .
  • The listener reads 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.