JavaScript, inside out · Chapter 19 of 51

Functions

By the end of this chapter you can write a rule once as a function, with defaults for what the caller leaves out, keep it from changing what it is given, and pick a function by name at run time. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: The order flow
  • 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

Kabir has found the same "price times cups minus discount" sum in nine places in Tapri's order code, and three of them are wrong. Meera wants one rule for the total that everyone calls. Anjali needs menu items built the same way every time, a cart that never changes behind her back, and receipts that stop coming out blank. Ravi wants a new payment method to take one line, not another branch in a long switch.

You have written functions in every task, but only the shape the starter gave you. Leave out the cups and 40 * undefined makes the bill NaN. A function that pushes into the cart it was given changes the cart on every screen. And a switch over payment methods grows with every new method.

What you build: 6 tasks

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

  1. Task 1

    orderTotal: an order total with default cups and discount

    Write orderTotal(price, cups, discount). Return the total in rupees: price times cups, minus a flat discount in rupees. cups is 1 and discount is 0 when they are missing, that is when they are undefined, whether left out or passed as undefined.

    So orderTotal(40) gives 40, orderTotal(40, 3) gives 120, orderTotal(40, 3, 20) gives 100 and orderTotal(40, undefined, 10) gives 30. 0 cups is a real number of cups: orderTotal(40, 0) gives 0.

  2. Task 2

    menuItem: a menu item from a one-line arrow

    Change menuItem(name, price) into a one-line arrow function that returns a new object with three keys: name, price, and label, which is the name, a colon and a space, then ₹ and the price. menuItem('bun maska', 35) gives { name: 'bun maska', price: 35, label: 'bun maska: ₹35' }.

    Every call returns a new object, so two calls with the same arguments give two different objects.

  3. Task 3

    addToCart: a new cart, the old one untouched

    Write addToCart(cart, name). cart is an array of items like { name: 'masala chai', qty: 1 }. Return a new cart with one more of name: if an item with that name is already in the cart, its qty goes up by 1 and it stays in its place; otherwise a new item { name, qty: 1 } is added at the end.

    The cart array and every item object in it must not change (the check freezes them): addToCart([{ name: 'masala chai', qty: 1 }], 'masala chai') gives [{ name: 'masala chai', qty: 2 }] and the cart you passed still says qty 1. The result is always a new array, even when nothing in it changed place, and an empty cart gives [{ name, qty: 1 }].

  4. Task 4

    pay: pay through a table of payment functions

    Fill paymentHandlers with two functions, upi and cash. Each takes an amount and returns a message: upi gives Paid ₹40 by UPI and cash gives Paid ₹40 in cash for an amount of 40.

    Then write pay(method, amount). It looks up the function for method in paymentHandlers and returns what it gives for amount: pay('upi', 40) gives 'Paid ₹40 by UPI'. When there is no function for that method, throw an Error whose message is Unknown payment method: followed by the method, like Unknown payment method: cheque.

    pay must look the method up in paymentHandlers each time it is called, so a handler added to the table later (say card) works without changing pay.

  5. Task 5

    comboLine: a combo with any number of prices

    Write comboLine(name, ...prices): a name followed by any number of prices. Return the name, a colon, then ₹ and the total of the prices, then for and how many items: comboLine('Rush combo', 40, 35, 10) gives 'Rush combo: ₹85 for 3 items'.

    Exactly one price says 1 item, not 1 items: comboLine('Just chai', 25) gives 'Just chai: ₹25 for 1 item'. With no prices at all, comboLine('Nothing') gives 'Nothing: ₹0 for 0 items'.

  6. Task 6 · interview question

    Interview: buildReceipt, fix the receipt that comes out undefined

    buildReceipt(order) is broken now: it always returns undefined. Fix it so it returns the receipt line Token 12: ₹90 by upi: the word Token, the order's token, a colon, then ₹ and the total, then by and the payment.

    The total is each item's price times its qty, all added up, and an order with no items has a total of 0: { token: 7, payment: 'cash', items: [] } gives 'Token 7: ₹0 by cash'.

What you will learn

  • Declarations, expressions and arrows: three ways to write a function, and what each returns.
  • Parameters and defaults: what happens to an argument that is left out, undefined, null or 0.
  • return: handing back a value, and the line break that hides it.
  • Pure functions and functions as values: change nothing you were given, and look a function up by name.

More in The order flow

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