Library › Coding AI › Software developers

Refactor an if-else chain into a lookup map, plus one new rule

Practice: Setting limits and rules. About 5 minutes.

About this exercise

Refactor shippingZone into a lookup object named ZONE_BY_COUNTRY of type Record<string, string>, declared outside the function with one country per line. Every current country must map the same way, ops just asked for Norway (NO) to be EU, anything unknown stays INTL, and the trim/uppercase step stays. Keep the exported signature, no if/else or switch left, no imports. Return the code in exactly 1 code block of at most 20 lines, with under 20 words of text.

Common mistakes when you ask AI for this

  • Letting the AI name it. The AI picks names like zoneMap or ZONES, which won't match what the admin report will import. Try: Say: "Name it ZONE_BY_COUNTRY, typed Record<string, string>, outside the function."
  • Missing the new country. The AI only converts what's there; it can't know ops added Norway. Try: Add: "Also map NO to EU."
  • Losing the default or the cleanup. Rewrites sometimes drop trim().toUpperCase() or return undefined for unknown countries. Try: Add: "Keep trim().toUpperCase(); unknown countries return INTL."
  • A rewrite with extras. The AI adds an enum, a Zone type or a new helper file, and explains it all. Try: Add: "Keep the signature, no imports, one code block, no text."

What a strong prompt includes

  • Names the object
  • Adds the new country
  • Keeps the default
  • Keeps the cleanup
  • Protects the signature

Practice this now ↑

Related exercises