JavaScript, inside out · Chapter 16 of 51

Spread and rest

By the end of this chapter you can use three dots to spread an array or object out into a new one or into a call, gather any number of arguments into an array, and change nested data without touching the original. 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

Anjali is building the cart screen and has been bitten twice this week. Adding a Parle-G to a customer's cart also showed it in the "previous cart" panel. Then changing Ravi's sugar preference changed it in yesterday's saved copy of his profile too. Meera wants one function that can bill a customer for two items or twenty, and the cheapest price on any list. And trays of snacks come in trays inside trays, which the kitchen wants as one flat list.

With what you know so far, push adds to the cart itself, so every panel holding that cart sees the change. Object.assign copies only the top level, so the nested preferences stay shared. And a function takes a fixed list of parameters: two items or twenty means passing an array and hoping every caller remembers to.

What you build: 5 tasks

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

  1. Task 1

    lowestPrice: the cheapest price, or null

    Write lowestPrice(prices). Return the lowest number in the prices array: lowestPrice([40, 25, 35]) gives 25. A list of one price gives that price, and 0 is a real price: lowestPrice([40, 0, 25]) gives 0, not null.

    An empty list gives null. Do not change the array you are given (the check passes a frozen one).

  2. Task 2

    billFor: a bill for any number of amounts

    Write billFor(customer, ...amounts). It is called with a customer and then any number of amounts, each a separate argument: billFor('Ravi', 40, 25, 40) gives { customer: 'Ravi', count: 3, total: 105 }, where count is how many amounts were passed and total is their sum.

    With no amounts, billFor('Anjali') gives { customer: 'Anjali', count: 0, total: 0 }.

  3. Task 3

    addToCart: a new cart with the item at the end

    Write addToCart(items, item). Return a new array with every item of the cart, in order, and then the new item at the end: the item object itself, not a copy. addToCart([], item) gives [item].

    The result is always a new array, and the cart you are given must not change (the check passes a frozen one, so push throws on it).

  4. Task 4

    updatePrefs: new preferences without touching the original

    Write updatePrefs(customer, changes). Return a new customer with every key of the old one, whose prefs are a new object: the old prefs with each key in changes applied on top. updatePrefs(ravi, { sugar: 'less' }).prefs gives { sugar: 'less', size: 'regular', kulhad: true }.

    The customer you are given, including its prefs object, must not change, and the new customer's prefs must be a different object from the old prefs, so changing them later does not reach the original either.

  5. Task 5 · interview question

    Interview: flatten, nested trays without flat

    Write flatten(items). Return a new array with every level of nesting removed, at any depth, and the values in their original order: flatten(['chai', ['bun', ['maska', ['Parle-G']]], 'kulhad']) gives ['chai', 'bun', 'maska', 'Parle-G', 'kulhad']. Empty arrays, at any depth, leave nothing behind, and flatten([]) gives [].

    The array's own flat method is not allowed: the check makes flat throw an error. The array you are given must not change, and the result is always a new array, even when nothing was nested.

What you will learn

  • Spread in arrays and objects: build a new array or object from old ones plus the change.
  • Spread in calls: hand an array to a function that wants separate values, like Math.min.
  • Rest parameters: gather any number of arguments into an array.
  • Nested data: what a spread shares, and walking nesting of any depth yourself.

More in The menu

Build "Spread and rest" 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.