Interactive calculator · Edition 2

Modify and customise

Change an interface without breaking the interaction.

Before you begin

A heading and paragraph in HTML. You do not need to know JavaScript beyond a variable and a function.

The practical problem

Build a two-number calculator for a learner checking everyday arithmetic. It must show that an empty input is missing, not treat it as zero.

Build it step by step

  1. Create and label two number inputs and an Add button.
  2. Read both input strings and first check that neither is empty. Convert the strings to numbers and display the result as text.
  3. Run the interactive preview. Change one style, then rerun the same inputs to check that appearance did not change the result.

Acceptance criteria

  • 2 + 3 gives 5, −2 + 3 gives 1, and 1.5 + 0.25 gives 1.75.
  • Missing input gives a plain-language error rather than a fabricated result.
  • The fields and button can be reached by keyboard in a sensible order.

Common mistake

Adding two strings may produce 23 instead of 5, and direct conversion of an empty string may incorrectly produce zero.

How to assess your work

Run the checks below and write down what you expected, what happened, and one change you can demonstrate. Self-checks are practice evidence, not a verified grade.

Build

Start with two number inputs and one Add button. Label the inputs clearly and display the total.

Test

Try 2 + 3, a negative number, a decimal and an empty input. Confirm empty input gives a helpful error. Try the operation using a keyboard.

Reflect

Which change affected presentation, and which affected behaviour?

Starter project and AI hint

Add two labelled number fields, an Add button, and a place to announce results.

Ask for test cases before asking for code. Include empty and negative inputs.