Set a Quantity with Limits

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

The problem

  • medium
  • Code

A quantity picker has a text input labelled "Quantity" showing 1, between buttons named "Remove one" and "Add one". The quantity stays between 1 and 10: Remove one is disabled at 1 and Add one at 10. You can also type a quantity. When you leave the field, it's tidied up: a number is rounded down to a whole number and brought within 1 to 10 (so 25 becomes 10, 0 becomes 1 and 2.5 becomes 2), and anything that isn't a number becomes 1. The buttons then carry on from the tidied number.

Your code must pass

  • It starts at 1 with Remove one disabled
  • The buttons change the quantity by one
  • Add one is disabled at 10
  • A typed number is tidied when you leave the field
  • Anything else becomes 1 when you leave the field