‹ back to the board

Tuesday, September 1

Apprenticelogic & control flow

The Watchword Carved on the Gate

The gate-reeve of bitwick keeps the town's night watchword carved on a slat of oak, and every new moon the watch proposes a replacement. Too many bad slats have been carved already, so the reeve wants each proposal judged before the chisel comes out.

Write gateCheck(word), which takes a single string word and returns a string: the name of the first rule the word breaks, testing the rules in exactly this order.

1. "length" — the word has fewer than 8 characters or more than 16 characters. 2. "case" — the word does not contain at least one uppercase letter (A–Z) and at least one lowercase letter (a–z). 3. "number" — the word contains no digit (0–9). 4. "mark" — the word contains some character that is not a letter (A–Z, a–z) and not a digit (0–9). 5. "stutter" — the word contains three identical characters in a row.

A word may break several rules; only the earliest one in that list is named. If it breaks none of them, return "open".

The word may be any string, including the empty string. The return value is always one of the six strings above.

  • input validation branches
  • Apprentice

examples

  1. case 1word of "thunder-KEG7""mark" — 12 characters, both cases present, a digit present, but the hyphen is neither letter nor digit
  2. case 2word of "aaaaaaaa""case" — it breaks both the case rule and the stutter rule, and the case rule is listed first
  3. case 3word of "Bellrope11""open" — two identical characters in a row are allowed
reward
220 XP
signature
gateCheck
not yet run

TypeScript, run as ES2022 — types are stripped, not checked. If a JS engine would run it, so will we.

loading the editor…
  • a word the gate accepts
  • too few characters
  • seventeen characters is too many
  • exactly eight characters is allowed
  • caseless word named before its stutter
  • no digit anywhere
  • missing digit outranks a stray mark
  • a character that is neither letter nor digit
  • three alike in a row
  • an empty slat
The Watchword Carved on the Gate · bitwick