Library › Coding AI › Software developers
Write a SKU regex that passes the tests plus a new rule
Practice: Giving the right context. About 5 minutes.
About this exercise
The SKU check is failing its tests. Last week the warehouse added Z as a valid region letter, and the tests don't cover it yet, so the regex must accept regions A, B or Z. It also needs named groups region and num. Reply with exactly 1 code block containing only the single line const SKU_RE = /.../; with no flags, plus under 40 words of explanation.
Common mistakes when you ask AI for this
- Leaving out the new region. The tests only show A and B, so the AI builds [AB] and rejects Z SKUs the warehouse now ships. Try: Add: "Region letters are A, B or Z (Z is new and not in the tests yet)."
- Forgetting the groups. skuParts() reads m.groups, but the AI won't add named groups unless asked. Try: Add: "Use named groups region and num."
- Getting test code and a tutorial. The AI likes to explain each regex piece and reprint the tests. Try: Say: "One code block with only the const SKU_RE line; under 40 words of explanation."
- Not anchoring. Without ^ and $, ' A-1042' and 'SKU A-1042' slip through. Try: Add: "Anchor with ^ and $; it must reject every mustNotMatch string."
What a strong prompt includes
- Mentions the new Z region
- Asks for named groups
- Says what to return
- Mentions anchoring or the tests
- Caps the explanation
Related exercises
- Add a cron line that matches the others in a crontab (Coding AI · Product managers)
- Add dark mode by overriding your existing CSS variables (Coding AI · Software developers)
- Add input checks to your number guessing game (Coding AI · High school students)
- Add input validation to a Java method with exact messages (Coding AI · Software developers)
- Add labels to a signup form without rewriting the page (Coding AI · High school students)