Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

Porting a match With Guards

Porting a match With Guards

Python’s match guards test conditions. A JavaScript switch compares its selector with each case value using strict equality. Putting guests > 100 in a case under switch (guests) compares a number with a Boolean, so Tom’s 120-guest booking reaches the default case and gets one caretaker.

For the Millbrook caretaker rules, use an else if chain in Python’s order. Test the missing guest count first, then the larger threshold, then the smaller one. A return ends the function as soon as its branch supplies the instruction.

Watch out: null and undefined are distinct values, and == null catches both a blank guest count and a missing field.

This final function produces the caretaker instructions for the five fixed bookings.

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

00:00 In this lesson, you’ll port the caretaker rules for Millbrook’s booking confirmations. Nadia needs the right staff for each event. The Python function uses match guards to make that choice.

00:12 Tom Becker has 120 guests. Python selects two caretakers. The larger counts need their own rules before the fallback of one caretaker. In the JavaScript port, switch holds the guest count, and each Python case has a corresponding case on the right.

00:52 The threshold expressions look like the guards you started with. Each return ends the function with its caretaker instruction, so these cases don’t need a break.

01:01 The calls below both functions use Tom’s same guest count. Keep the cases in Python’s order for this first port. What caretaker instruction does this JavaScript call produce for Tom?

01:14 Your turn: pause the video and predict the printed instruction.

01:24 One caretaker. The JavaScript run finishes without an error, but Tom’s wedding now has one caretaker instead of two. Maya’s 80-guest party also gets one caretaker, instead of a caretaker and a door steward.

01:42 An ordinary JavaScript switch compares its value with each case using strict equality. Tom’s guest count stays the number 120. The first threshold expression evaluates to true.

01:56 That becomes the case value. JavaScript compares the number with that Boolean. A number and true aren’t strictly equal, so this case doesn’t match.

02:08 The next condition also evaluates to true for 120 guests. The switch still compares that Boolean with the original number. It doesn’t match either.

02:21 With no matching case, default supplies one caretaker. Python’s guard asks whether its condition holds; this JavaScript case supplies a value for comparison.

02:33 To express the staffing conditions directly, use a chain in the same order. In JavaScript, an else if chain tries the next condition only when the earlier one was false.

02:53 Keep the missing-count check first, then the larger threshold, then the smaller one. Watch out: null and undefined are distinct values, and the familiar == null check catches both a blank guest count and a missing field.

03:09 For Tom, the first size condition is true, so the function returns two caretakers. That keeps his count out of the lower branch.

03:19 Run the final function for the week’s five bookings. The choir gets one caretaker. Maya gets one caretaker and a door steward. Tom gets two caretakers. Tuesday Yoga gets one caretaker.

03:39 Ana’s blank count gets guest count missing: call the hirer. All five instructions now match Python’s. The two larger events have the staffing their guest counts require.

03:53 switch (true) works too: its Boolean selector can match the conditions’ Boolean results, and some codebases use that form. For these rules, most JavaScript developers write the chain because it states the conditions directly.

04:14 Keep switch for fixed values; keep the chain for these ordered conditions.

04:27 Next up, Porting a match With Patterns: you’ll translate the booking notice rules, including what to do when one room is selected.

Become a Member to join the conversation.