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 the Garden Season Report

Porting the Garden Season Report

You’ve ported Rosa’s script one part at a time. In this project, most of it goes into one file for the booking page.

A port that runs can still offer plot 3 to the wrong person. So each dict, set and counter is written the JavaScript way, and the port should print Python’s first ten lines. The seed swap lines are left for the exercise.

  • The section and show class tuples become frozen arrays, with Object.freeze(). A plot’s fields are names you write, so it stays an object, and its two crop sets, crops and lastYear, turn into Sets.
  • The waiting list keeps member numbers in the order people asked, so it becomes a Map. An object would sort the numbers and offer the plot to June Harris, member 42. The new applicants and the work days follow: a Map and two Sets.
  • JavaScript has no set comprehension, so the port adds each holder’s name to an empty Set in a for...of loop, until array methods come later.
  • Each set operator becomes a method that returns a new Set: union(), intersection() and difference(). The pipe, |, turns Sets into numbers, so the turnout would read undefined of 9. .size stands in for len(), and each result is spread into an array and sorted before it’s joined.
  • Python compares each plot’s crops with last year’s using ==. JavaScript’s === asks whether two Sets are the same object, so no plot would match. The port calls a helper, sameMembers(), which checks the size, then whether one Set is a subset of the other. The loop pushes each matching plot’s number.
  • Plot 4’s crops went in as potatoes, then kale, and last year’s as kale, then potatoes. sameMembers() still counts plot 4 as the same: a Set keeps the order its members went in, but isSubsetOf() only asks about members, as Python’s == does.
  • Python merges the applicants in with |=. The port loops over their Map and sets each pair, and the square brackets in the loop header destructure it, like tuple unpacking.
  • The first key, [...waiting.keys()][0], replaces next(iter(waiting)), and pop() becomes get(), then delete(). With brackets, Dev Patel would stay listed and Ola Bakare never join. Then a loop finds plot 3 and gives it to Dev Patel, with an empty Set of crops.
  • Python’s defaultdict(list) becomes Map.groupBy(), which groups the whole plots array in one call. The append loop, ported line by line, would stop on the first plot.
  • Section D has nobody yet, so its group is missing, and the lookup falls back to an empty array with ?? []. D prints zero plots, as in Python.
  • Python’s Counter becomes a Map of counts, each read as the stored number or zero. One loop over the show classes builds the counts line, and a second picks the classes under three.
  • Unlike a Counter, a Map has no zero for a missing class. Read without the default, runner beans would print undefined and never be canceled.

Here’s the port, garden-report.js, with the seed swap still to come:

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:02 You’ve ported Rosa’s script one part at a time. Now most of it goes into one file for the booking page. A port that runs can still offer plot three to the wrong person.

00:15 So each dict, set and counter is written the JavaScript way, and the port should print Python’s first ten lines. The seed swap lines are left for the exercise.

00:27 The section and show class tuples become frozen arrays. A plot’s fields are names you write, so it stays an object, and its two crop sets turn into Sets. The waiting list keeps member numbers in the order people asked, so it becomes a Map.

00:44 An object would sort the numbers and offer the plot to June Harris. The new applicants and the work days follow: a Map and two Sets.

00:56 JavaScript has no set comprehension, so the port adds each holder to an empty Set in a for...of loop, until array methods come later. Each set operator becomes a method that returns a new Set.

01:09 The pipe, |, turns Sets into numbers, so the turnout would read undefined of nine. .size stands in for len(), and each result is spread into an array and sorted before it’s joined.

01:24 Python compares each plot’s crops with last year’s using ==. JavaScript’s === asks whether two Sets are the same object, so no plot would match.

01:36 The port calls a helper, sameMembers(), which checks the size, then whether one Set is a subset of the other. The loop pushes each matching plot’s number.

01:49 Look at plot four. Its crops went in as potatoes, then kale, and last year’s as kale, then potatoes. Does sameMembers() count plot four? Your turn: pause the video and predict.

02:08 Yes, and the line names plot four. A Set keeps the order its members went in, but isSubsetOf() only asks about members, as Python’s == does.

02:22 Python merges the applicants in with the update operator, |=. The port loops over their Map and sets each pair, and the square brackets in the loop header destructure it, like tuple unpacking.

02:35 The first key replaces next(iter()), and pop() becomes get(), then delete(). With brackets, Dev Patel would stay and Ola never join.

02:45 A loop finds plot three and gives it to Dev Patel, with an empty Set of crops.

02:54 Python’s defaultdict(list) becomes Map.groupBy(), which groups the whole plots array in one call. The append loop, ported line by line, would stop on the first plot.

03:07 Section D has nobody yet, so its group is missing, and the lookup falls back to an empty array. D prints zero plots, as in Python.

03:19 Python’s Counter becomes a Map of counts, each read as the stored number or zero. One loop over the show classes builds the counts line, and a second picks the classes under three.

03:31 Unlike a Counter, a Map has no zero for a missing class. Read without the default, runner beans would print undefined and never be canceled.

03:44 node --check reads the file and says nothing: no syntax errors. Run it, and JavaScript prints ten lines. They match Python’s first ten, line for line.

03:58 The four seed swap lines are still Python’s alone.

04:03 The rule: port what each dict and set means, then check that the output matches. Records stay objects, lookups become Maps, and set operations become methods.

04:16 Next up: the exercise Finish the Season Report. You’ll add the last four lines: the crops each section grows, in alphabetical order, with nothing yet for section D and without plot three’s old crops.

04:31 JavaScript then prints Python’s fourteen lines, for this season and another the tests make up.

Become a Member to join the conversation.