Command Palette

Search for a command to run...

Hectal
Phase 14: Advanced Language
JavaScript · Exercise 4 of 5 PreviousNext
Advanced

Settings guarded by a Proxy

Write createSettings() that returns a Proxy around { volume: 50, theme: "light" } with these rules for writes:

volume must be a whole number from 0 to 100, otherwise throw a RangeError. theme must be "light" or "dark", otherwise throw a TypeError. Writing any other property throws a TypeError, and nothing is stored when a write is rejected.

Show the starting settings in #status as Volume 50, theme light.

What Check tests

  1. createSettings() starts with volume 50 and theme light
  2. Valid writes are stored
  3. Bad volumes throw a RangeError and aren't stored
  4. A bad theme throws a TypeError
  5. Unknown properties are rejected
  6. Uses a Proxy
  7. The page shows Volume 50, theme light

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

Runs in your browser. Your code is saved here.

Your page

Press Run to see the page.

Console

Nothing logged yet.