JavaScript, inside out · Chapter 10 of 51

Arrays

By the end of this chapter you can keep a list in one value: read any position, add and remove at both ends, copy or cut a piece, and find an item. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: The menu
  • 5 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

Meera points at the paper menu: "Masala chai, cutting chai, bun maska, Parle-G. It is a list. The orders are a list. The kitchen queue is a list." Ravi needs that queue for the 4 pm rush: orders join at the back, an office order for ten chai jumps to the front, the kitchen serves from the front, and a customer at the back sometimes cancels. Anjali wants the menu in pages for a small screen, and a sold-out item swapped for its replacement. And a double tap sends the same order twice.

With what you know so far, each item needs its own variable (item1, item2, item3), and a new item means new code. Or you keep the list as text with commas, as in chapter 5, and split and join it again for every change. Neither can put an order at the front of a queue.

What you build: 5 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    menuEnds: the first item, the last and the count

    Write menuEnds(items). Return an object with three keys: first (the first item), last (the last item) and count (how many items there are). menuEnds(['Masala chai', 'Bun maska', 'Parle-G']) gives { first: 'Masala chai', last: 'Parle-G', count: 3 }.

    A menu of one item gives that item as both first and last, and an empty menu gives { first: undefined, last: undefined, count: 0 }. Do not change the array you are given: the check passes a locked one.

  2. Task 2

    runQueue: the kitchen queue through a rush

    Write runQueue(events). Start with an empty queue and go through the events in order. An order event's item joins the back of the queue. An urgent event's item jumps to the front. A serve event takes the item at the front out of the queue and serves it. A cancel event drops the item at the back, which is not served. Serve and cancel do nothing when the queue is empty.

    Return { served, waiting }: served holds the served items in the order they were served, and waiting is what is still in the queue, front first. runQueue([{ type: 'order', item: 'chai' }, { type: 'urgent', item: 'bun' }, { type: 'serve' }]) gives { served: ['bun'], waiting: ['chai'] }.

    No events gives { served: [], waiting: [] }. Every call starts with an empty queue, and the events array must not change.

  3. Task 3

    menuPage: one page of the menu

    Write menuPage(items, page, size). Pages are numbered from 1 and hold size items each, in menu order. Return a new array with the items on that page: menuPage(['a', 'b', 'c', 'd', 'e'], 2, 2) gives ['c', 'd'].

    The last page may be shorter: menuPage(['a', 'b', 'c', 'd', 'e'], 3, 2) gives ['e']. A page past the end gives []. The menu must not change: the check passes a locked one.

  4. Task 4

    swapSoldOut: swap a sold-out item in place

    Write swapSoldOut(items, soldOut, replacement). Change the menu array itself, not a copy: put the replacement in the sold-out item's position, or, when no replacement is given, remove the sold-out item so that nothing is left in its place. Return the removed item itself, a string, not an array.

    When the sold-out item is not on the menu, return null and leave the menu as it is.

    With m = ['Masala chai', 'Bun maska', 'Parle-G'], swapSoldOut(m, 'Bun maska', 'Vada pav') returns 'Bun maska', and m becomes ['Masala chai', 'Vada pav', 'Parle-G'].

  5. Task 5 · interview question

    Interview: uniqueOrders, each order once

    Write uniqueOrders(orders). Return a new array with each order once, in the order it was first seen: uniqueOrders(['chai', 'bun', 'chai', 'Parle-G', 'bun']) gives ['chai', 'bun', 'Parle-G'].

    Two orders are the same only when they are exactly equal, so 'masala chai' and 'Masala chai' are two different orders. An empty list gives []. The array you are given must not change (the check passes a locked one), and the result is always a new array.

What you will learn

  • Positions, length and at: read any item, from the front or the back.
  • push, pop, unshift and shift: add and remove at either end.
  • slice and splice: copy a piece, or cut the array itself.
  • includes, indexOf and for...of: find an item, and visit every item in turn.

More in The menu

Build "Arrays" 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.