Porting a match With Patterns

You’ve ported the guards, so Nadia’s caretakers know how many staff members each booking needs. Next comes the caretaker’s notice. Python picks it by the shape of the request, but a JavaScript switch only compares values with ===, so you can’t port these patterns directly:

Language: Python
def notice_for(r):
    match r:
        case {"kind": "wedding" | "party"}:
            return "music off by 11 p.m."
        case {"kind": "class" | "meeting", "rooms": [room]}:
            return f"{room} set up 15 minutes early"
        case {"kind": "class" | "meeting"}:
            return "rooms set up 15 minutes early"
        case {"kind": kind}:
            return f"check the {kind} request by hand"

To keep the same notices, the port names the property each pattern tests and asks about the rest in plain conditions.

Turn Each Pattern Into a Check

The form’s export gives every request a kind and a rooms array, so for these records each pattern has a direct counterpart in JavaScript:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.