JavaScript, inside out · Chapter 11 of 51
Array methods
By the end of this chapter you can answer questions about a whole list in one line: keep some items, change every item, add them up, find one, and check them all. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: The menu
- 6 tasks
- JavaScript
- In the all labs plan
The first 2 chapters of JavaScript, inside out are free. This one comes with the all labs plan.
Why you need this
Anjali wants the menu board to show only what is in stock, with prices. Ravi wants the total of every order, one kitchen ticket for every cup, and at the end of the 4 pm rush two quick answers: has anyone not paid, and has everything gone out? Meera wants the orders grouped by customer, because the loyalty card starts next week.
With what you know from the last chapter, you would read the list one index at a time: look at menu[0].inStock, then menu[1].inStock, and push a label for each one that is in stock. That is four checks for four items today. The day Ravi adds vada pav, your code still checks four, and the new item silently never reaches the board.
What you build: 6 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
availableLabels: menu labels for what is in stock
Write availableLabels(items). Return a new array with one label for every item that is in stock, written like Masala chai: ₹40 (the name, a colon and a space, then ₹ and the price). Leave out the items that are sold out and keep the menu order. Do not change the array you are given.
When nothing is in stock, return an empty array.
- Task 2
orderTotal: the total of one order
Write orderTotal(order). It takes one order and returns its total in rupees: for every item in the order's items, its price times its qty, all added up.
An order with no items has a total of 0.
- Task 3
lookupOrder: an order and its position
Write lookupOrder(list, id). Find the order whose id equals the id you are given, and return an object with two keys: order (the order itself, the same object that is in the list, not a copy) and position (its index in the list).
When no order has that id, return order undefined and position -1.
- Task 4
rushCheck: anyone unpaid, everything delivered?
Write rushCheck(list). Return an object with two true or false answers: anyUnpaid is true when at least one order is not paid, and allDelivered is true when every order is delivered.
With no orders at all, nobody owes money and nothing is waiting to go out, so the answer is anyUnpaid false and allDelivered true.
- Task 5
kitchenTickets: one ticket per cup
Write kitchenTickets(list). Return one flat array of tickets, one ticket per cup: the order's id, a colon and a space, then the item's name, like 101: Masala chai. An item with qty 2 makes two tickets.
Keep the orders, and the items inside each order, in their original order. An order with no items makes no tickets, an empty list gives an empty array, and the list you are given must not change.
- Task 6 · interview question
Interview: groupByCustomer, orders by customer with reduce
Write groupByCustomer(list) with reduce. Object.groupBy is not allowed here: the check makes it throw.
Return an object with one key per customer name, in the order each customer first appears in the list. Each key holds an array of that customer's orders (the same order objects, not copies), in their original order.
An empty list gives an empty object. Every call starts from a fresh object, so a second call does not see the first call's orders. The list you are given must not change.
What you will learn
- filter and map: keep the items that pass a test, and turn every item into something new.
- reduce: walk the list carrying one value along, down to a single answer.
- find, findIndex, some and every: find one item, or ask a yes or no question about all of them.
- flat and flatMap: turn a list of lists into one list.
More in The menu
Build "Array methods" yourself
Tapri, a chai brand going online, needs its menu, orders and kitchen in code. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of JavaScript, inside out are free. This one comes with the all labs plan.