Combining Sets With union() and intersection()
Every plot holder at the garden should come to one of the two work days. In this lesson, you ported the lines of Rosa’s script that check, from Python’s set operators to JavaScript’s Set methods.
Here are the lines. The plot records keep only each plot’s number and holder:
plots = [
{"plot": 1, "holder": "Ana Lima"},
{"plot": 2, "holder": "Ben Carter"},
{"plot": 3, "holder": "Chloe Park"},
{"plot": 4, "holder": "Dara Quinn"},
{"plot": 5, "holder": "Eli Ross"},
{"plot": 6, "holder": "Fatima Sheikh"},
{"plot": 7, "holder": "Gus Tanaka"},
{"plot": 8, "holder": "Hana Uddin"},
{"plot": 9, "holder": "Ivan Volkov"},
]
spring = {
"Ana Lima", "Ben Carter", "Chloe Park",
"Dara Quinn", "Gus Tanaka", "Hana Uddin",
}
autumn = {
"Ben Carter", "Eli Ross", "Gus Tanaka", "Ivan Volkov",
}
holders = {p["holder"] for p in plots}
helped = spring | autumn
both = spring & autumn
missed = holders - helped
thanks = ", ".join(sorted(both))
print(f"Thanks for coming to both: {thanks}")
reminder = ", ".join(sorted(missed))
print(f"Work day reminder: {reminder}")
spring | autumnis everyone who helped,spring & autumnis the people who came to both, andholders - helpedis who missed both. Python thanks Ben Carter and Gus Tanaka, and reminds Fatima Sheikh.- Ported line by line, each set display becomes
new Set()around an array. JavaScript has no set comprehension, so afor...ofloop over the plots adds each holder. The three operator lines are copied unchanged, withconstin front. - Logged before the report lines are ported,
helpedandbothare 0 and 0,missedis NaN, andhelped.sizeis undefined. There’s no error and no warning: every line ran, and none of them made a Set. - In both languages,
|and&between two whole numbers are bitwise operators. Python’ssettype gives them a second meaning, but JavaScript has no way for a type to do that, so they only work on numbers. - Each Set turns into text,
"[object Set]", which isn’t a number, so it’s NaN. Bitwise operators count NaN as 0, and0 | 0is 0.-subtracts, and a Set minus 0 is NaN.helpedis then the number 0, and a number has nosize, sohelped.sizeis undefined. - JavaScript names each set operation as a method instead:
spring.union(autumn)for the bar andspring.intersection(autumn)for the ampersand.helpedis a new Set of all eight names, spring’s first, then autumn’s two new ones, andbothholds Ben Carter and Gus Tanaka. Each method leavesspringandautumnalone, as Python’s operators do. These methods are recent additions to JavaScript, and Node 26 has them all. - For the minus,
holders.difference(helped)keeps the holders who aren’t inhelped: one name, Fatima Sheikh. - Watch out for lists: Python’s
union()takes a list too, but these methods want a Set. Given an array, they stop with aTypeErrorabout itssize, so wrap the array innew Set()first. - A JavaScript Set keeps insertion order, while Python’s sets promise no order: printed as it is,
bothcan come out either way from one run to the next. So Python’s script sorts before it joins, and the port sorts too, since insertion order follows the lists, not the alphabet. A Set has notoSorted(), so spread it into an array, sort the copy, and join it with commas.