‹ back to the board

Sunday, September 13

Apprenticelogic & control flow

The Night-Warden's Watchwords

The night-warden of bitwick will not open the postern for just any word. She keeps a slate of five rules, ranked, and she reads them from the top down: the first rule a word breaks is the one she writes in her book, and she reads no further.

Given word, a string, return a string naming the outcome.

The rules, in this exact order:

1. "length" — the word must be at least 6 and at most 12 characters long. 2. "charset" — every character must be a lowercase letter a–z or a digit 0–9. 3. "start" — the first character must be a lowercase letter a–z. 4. "digits" — the word must contain at least two digit characters. 5. "repeat" — no character may appear three or more times in a row.

Return the name of the first rule in this list that the word breaks. If the word breaks none of them, return "pass".

  • input validation branches
  • Apprentice

examples

  1. case 1word of "mead77""pass" — six characters, all allowed, starts with a letter, two digits, no run of three
  2. case 2word of "barley""digits" — it passes the first three rules but carries no digits at all
  3. case 3word of "7Ale""length" — it is also outside the allowed characters and starts with a digit, but length is the earliest rule it breaks
  4. case 4word of "hoggg22""repeat" — three g's in a row
reward
220 XP
signature
checkWatchword
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…
  • empty word fails on length
  • clean six-character word passes
  • no digits at all
  • only one digit
  • uppercase letter is out of charset
  • space is out of charset
  • begins with a digit
  • three identical letters in a row
  • three identical digits in a row
  • twelve characters is still allowed
  • thirteen characters is too long
  • length outranks every later fault
  • alternating characters are not a run
  • pairs are fine, triples are not
The Night-Warden's Watchwords · bitwick