Your switch port of notice_for() gives Nadia’s five notices, but every case only computes a string from the request. Python code often keeps cases like these in a dict of functions instead. Here’s how to port that lookup and what JavaScript needs before it calls a function that might not be there.
Keep a Function Per Kind
A lookup object holds values under keys you write in the code. The event kinds become its keys, and arrow functions such as (r) => ... become its values. Here’s the Python next to the JavaScript:
| Python | JavaScript | |
|---|---|---|
| The lookup | NOTICES = {"wedding": music_off, ...} |
const NOTICES = { wedding: musicOff, ... }; |
| Read an unlisted kind | NOTICES.get("market") is None |
NOTICES["market"] is undefined |
The lookup is where the two differ. In Python, NOTICES["market"] raises KeyError at the lookup, which is why you reach for .get(). In JavaScript, reading a key the object doesn’t have gives undefined instead of failing, so the error waits for the call:
const musicOff = () => "music off by 11 p.m.";
const setUpEarly = (r) => r.rooms.length === 1
? `${r.rooms[0]} set up 15 minutes early`
: "rooms set up 15 minutes early";
const NOTICES = {
wedding: musicOff,
party: musicOff,
class: setUpEarly,
meeting: setUpEarly,
};
const r = { kind: "market", rooms: ["hall"] };
console.log(NOTICES[r.kind](r));