Handle a Missing Map Key Safely
Scenario
Room map: { "Room-A": 2, "Room-B": 0 }. Look up three room codes.
Try it yourself
Compare a known open room, a known full room, and a missing room code.
| Case | Result |
|---|---|
| Lookup "Room-A" | Select a choice above to see what happens. |
| Lookup "Room-B" | Select a choice above to see what happens. |
| Lookup "Room-Z" (not in map) | Select a choice above to see what happens. |
Reveal the explanation
- Lookup "Room-A"
Key exists -> returns 2 free seats (Accept).
- Lookup "Room-B"
Key exists with value 0 -> returns "Full" (NOT "Unknown room").
- Lookup "Room-Z" (not in map)
Key absent -> returns explicit "UNKNOWN_ROOM" status instead of crashing.
Always check whether the key exists in the map first. Returning 0 for a missing room blurs "Room-B is full" with "Room-Z does not exist".
Download shared test cases
Your turn
Given equipmentStock = { "MIC-1": 0, "CAM-2": 3 }, state the exact return values for "MIC-1", "CAM-2", and "PROJ-9".
Common mistake Returning 0 for "PROJ-9" so a typo looks identical to an out-of-stock microphone.
Check your work
MIC-1 -> 0 (out of stock); CAM-2 -> 3 (available); PROJ-9 -> UNKNOWN_ITEM.
Quick check
This browser could not save your progress. Changes last until you leave the page.