JavaScript, inside out · Chapter 14 of 51

Object methods

By the end of this chapter you can turn an object into arrays and back, merge objects without changing them, lock an object at one level or at every level, and copy one so that nothing is shared. 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

The menu now lives in one object, names to prices. Anjali wants lines for the menu board from it, and Meera wants to try a 10% price rise on a copy before she decides. Each customer's chai settings should be filled in from the house defaults. Ravi wants bills that can change but never grow a stray field, and the menu locked so nobody changes a price by accident at 4 pm. And yesterday a "draft" order changed the real order it was copied from.

With what you know so far, you can read an object one key at a time, and that is it. None of the array methods from chapter 11 work on an object, const does not stop anyone changing a price, and a copy made key by key is easy to get half right.

What you build: 6 tasks

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

  1. Task 1

    menuLines: menu board lines from the price object

    Write menuLines(prices). prices is an object of item names and prices, like menu. Return an array with one line per item, in the order of the object's keys, written like Masala chai: ₹40 (the name, a colon and a space, then ₹ and the price).

    An empty object gives []. Do not change the object you are given (the check passes a frozen one).

  2. Task 2

    applyHike: a price rise on a new menu

    Write applyHike(prices, percent). Return a new object with the same keys in the same order, and every price raised by percent percent, rounded to the nearest rupee with Math.round (a half rounds up): applyHike({ 'Masala chai': 40, 'Cutting chai': 25 }, 10) gives { 'Masala chai': 44, 'Cutting chai': 28 }, because 27.5 rounds up to 28.

    A rise of 0 gives the same prices. The object you are given must not change (the check passes a frozen one) and is never the one returned.

  3. Task 3

    withDefaults: chai settings filled in from the defaults

    Write withDefaults(settings). Return a new object with every key of DEFAULTS (sugar, size, kulhad and extraGinger), where any key in settings replaces the default: withDefaults({ sugar: 'less' }) gives { sugar: 'less', size: 'regular', kulhad: false, extraGinger: false }.

    Neither DEFAULTS nor settings may change (the check passes frozen settings), and the result is never the DEFAULTS object itself. A second customer must get the real defaults, not the first customer's choices: withDefaults({}) gives a copy of DEFAULTS.

  4. Task 4

    openTab: a bill that can change but cannot grow

    Write openTab(customer). Return a new bill { customer, total: 0, items: [] } that is sealed: its existing keys can change (tab.total = 65 works), but adding a key or deleting one throws a TypeError. It must be sealed, not frozen.

    Every call makes a new bill with its own items array, so one customer's items never show up on another's bill.

  5. Task 5

    deepFreeze: the menu frozen all the way down

    Write deepFreeze(obj). Freeze obj and every object and array inside it, at any depth: items inside arrays, arrays inside objects, objects inside objects. Return obj itself, the same object, not a copy.

    Values that are not objects (text, numbers, true or false) are left as they are, and so is null: the check's menu has a key holding null.

  6. Task 6 · interview question

    Interview: draftFromOrder, fix the draft that changes the real order

    draftFromOrder(order) is broken now: making a draft also changes the real order. Fix it. Once fixed it returns a draft: a copy of the order with { name: 'Parle-G', price: 10, qty: 1 } added at the end of its items and status 'draft'.

    The order you are given must not change at all, including its items and its customer. And the draft must share nothing with it: changing the draft's customer address or one of its items later must not reach the order either.

What you will learn

  • Object.keys, values, entries and fromEntries: from an object to arrays, and back.
  • Object.assign: copy keys from one object onto another.
  • Object.freeze and Object.seal: lock an object, fully or in part.
  • Shallow and deep copies: what a copy shares, and structuredClone.

More in The menu

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