Shift-click to Select a Range

Write the code, see it run, pass the checks.

The problem

  • hard
  • Code
  • Pro

An inbox lists six messages as checkboxes labelled "Mon", "Tue", "Wed", "Thu", "Fri" and "Sat", all unticked, above a line reading "0 selected" (counting the ticked boxes). A plain click ticks or unticks one box. Holding Shift while clicking selects a run, as in an email app: the clicked box flips, and every box from the one clicked just before it (with or without Shift) to this one, both included, takes the same new state. For example, tick Tue, then Shift-click Fri, and Tue to Fri are all ticked: "4 selected". A Shift-click before any other click just flips that one box.

Your code must pass

  • Plain clicks tick and untick one box
  • Shift-click ticks the run from the last click
  • Shift-click works upwards and can untick a run
  • Shift-click with no earlier click flips just that box