Counting Show Entries Without Counter

The waiting list is settled, and Rosa’s report moves on to the harvest show. The show has five classes, and twelve entries came in, though nobody entered runner beans. The committee wants a line with each class’s count. Any class with fewer than three entries gets canceled, so those counts have to be right.

Rosa’s Python script counts the entries with one call to Counter. JavaScript has no Counter, so here you’ll write the counting loop yourself and pick out the most popular classes. You’ll finish the report, each holder’s entries and the canceled classes, in the exercise Prepare the Harvest Show Report. Start with the loop that Counter was saving you from.

Count Into a Map One Entry at a Time

In Python, each show entry is a dict with a holder and a show class, and Counter does all the counting:

Language: Python
entries = Counter(e["show_class"] for e in show_entries)
print(entries)

It prints Counter({'potatoes': 4, 'jam': 3, 'onions': 3, 'marrow': 2}). Before Counter, a Pythonista wrote the loop by hand, reading each count with a default of 0 and adding one:

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.