Full-Stack course / JavaScript fundamentals
Group orders by customer
How to fetch and submit this exercise
Once: install the tool and connect your account.
curl -fsSL https://gonomicon.com/install.sh | sh
gonomicon loginEach exercise: download it, edit the file named in the instructions, then submit.
gonomicon fetch fs-week-2/03-08-group-orders-by-customer
cd 03-08-group-orders-by-customer
gonomicon submitGroup orders by customer
Goal: turn a flat list of orders into a Map with one entry per customer, showing how many orders they placed and how much they spent in total.
Worked example
Each order is an object { customer, amount }. For this list:
[
{ customer: "Ana", amount: 20 },
{ customer: "Ben", amount: 5 },
{ customer: "Ana", amount: 10 },
]
groupByCustomer(orders) returns a Map that holds:
| Key (customer) | Value |
|---|---|
"Ana" | { count: 2, total: 30 } |
"Ben" | { count: 1, total: 5 } |
An empty list gives an empty Map (its size is 0).
Rules
- Write
function groupByCustomer(orders)as a function declaration. It takes the list of orders and returns aMap. - Each key is a customer's name, exactly as written in the orders.
"ana"and"Ana"are two different customers. - Each value is an object with exactly two properties:
count(how many orders that customer placed) andtotal(the sum of theiramounts). - An order with an
amountof0still counts as an order. - Customers appear in the Map in the order they first show up in the list.
- Don't change the
orderslist or the order objects inside it. Build new objects for the result. - The tests also try long lists, so walk the orders only once.
- To try it yourself, add a line such as
console.log(groupByCustomer([{ customer: "Ana", amount: 20 }]));at the bottom while you work, then remove it, and runnode group_orders_by_customer.js.
What you'll practice
- Grouping: one pass over the data, one bin per group
- Using a Map to hold the bins
- Updating a bin without changing the original data
- Falling back with
??the first time a customer is seen
Run it
gonomicon fetch fs-week-2/03-08-group-orders-by-customer
cd 03-08-group-orders-by-customer
gonomicon test
gonomicon submit
Hints
- You need one Map entry per customer, so the customer name is the key. Walk the orders once with
for...of, and for each order look up what that customer already has. groups.get(name)gives you the customer's current{ count, total }, orundefinedthe first time you see them. Use??to start a new customer at{ count: 0, total: 0 }, thengroups.set(name, ...)the updated numbers.- Shape:
const groups = new Map(); for (const { customer, amount } of orders) { const entry = groups.get(customer) ?? { count: 0, total: 0 }; groups.set(customer, { count: entry.count + 1, total: entry.total + amount }); } return groups;