Counting Show Entries Without Counter

With the waiting list settled, Rosa’s report moves on to the harvest show. It has five classes and twelve entries, and nobody entered runner beans. The report later cancels any class with fewer than three entries, so the counts have to be right. Rosa’s script leans on Counter, and JavaScript has no equivalent, so you write the counting loop yourself.

Count Into a Map With get() and ??

Counting is still one pass over the entries. Port the handwritten dict loop, counts[c] = counts.get(c, 0) + 1, line by line, and you keep Python’s default inside the call:

Language: JavaScript
const showEntries = [
  { holder: "Ana Lima", showClass: "potatoes" },
  { holder: "Ana Lima", showClass: "jam" },
  { holder: "Ben Carter", showClass: "onions" },
  { holder: "Ben Carter", showClass: "potatoes" },
  { holder: "Dara Quinn", showClass: "potatoes" },
  { holder: "Dara Quinn", showClass: "marrow" },
  { holder: "Eli Ross", showClass: "onions" },
  { holder: "Eli Ross", showClass: "jam" },
  { holder: "Gus Tanaka", showClass: "potatoes" },
  { holder: "Gus Tanaka", showClass: "onions" },
  { holder: "Hana Uddin", showClass: "jam" },
  { holder: "Hana Uddin", showClass: "marrow" },
];

const entries = new Map();
for (const entry of showEntries) {
  const c = entry.showClass;
  entries.set(c, entries.get(c, 0) + 1);
}
console.log(entries);

Python’s loop gives potatoes 4, jam 3, onions 3, and marrow 2. Predict the Map before you read the output:

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.