CodingNic

Calculator Controls

Implement Clear

Calculator Controls 15 min read

Implement Clear

Task

Connect the prepared Clear button to the clear() method you created in Module 1.

Open

Open:

js/ui.js

Add the clear action to the existing button handler.

javascript
if (action === 'clear') {
  calculator.clear();
}

Then update the display as usual:

javascript
updateDisplay();

Test

In the browser:

  1. Enter 123.
  2. Press +.
  3. Enter 45.
  4. Press C.

The display should return to 0.

Use the browser console to confirm that the calculator state has also been reset.

Checkpoint

The Clear control now resets the calculator through one centralized method.