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".
examples
- case 1
word of "mead77""pass" — six characters, all allowed, starts with a letter, two digits, no run of three - case 2
word of "barley""digits" — it passes the first three rules but carries no digits at all - case 3
word of "7Ale""length" — it is also outside the allowed characters and starts with a digit, but length is the earliest rule it breaks - case 4
word of "hoggg22""repeat" — three g's in a row
- reward
- 220 XP
- signature
checkWatchword
TypeScript, run as ES2022 — types are stripped, not checked. If a JS engine would run it, so will we.
- 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