Brackets
Brackets nest like packaging: i( opens the layer around the cursor, 2i( the next one out; i[, i{ and i< are their own wrappings, and di{ empties a body.
Tuesday, 2:00 p.m. The price calculator throws on the new menu file. Its arguments were written for the old one: subtotal wants the order now, deliveryFee wants the zone, and everything is a call inside a call inside a call.
You'll be changing what's inside brackets all afternoon, often brackets inside other brackets.
Warm-up from earlier chapters
Two from earlier chapters, from memory, before the new idea. Two wrong tries show the answer.
Layers of packaging
Brackets nest like a delivery: the burrito in its foil, in a paper bag, in the box. With the cursor anywhere in subtotal(order), i( opens the layer you're in and takes order; a( takes it with its brackets. A count goes further out: 2i( is the next pair out, everything withTax gets, and 3i( is everything inside round.
const total = round(withTax(subtotal(order), rate), 2);const total = round(withTax(subtotal(order), rate), 2);const total = round(withTax(subtotal(order), rate), 2);const total = round(withTax(subtotal(order), rate), 2);New arguments
Three calls with arguments from the old file. ci( from anywhere between the brackets, and type the new ones.
This exercise needs a keyboard. Read on, and play it at your computer.
Two layers out
The cursor is in order. You press d2i(. What does the line become?
Different wrappings
Each kind of bracket is its own wrapping, and its own object: i[ for square brackets, i{ for braces, i< for angle brackets. i( ignores square brackets and braces completely. Two shortcuts: ib is the same as i( and iB is i{; Vim calls them a block and a Block.
render({ tags: ["pork", "spicy"], max: limit(3) });render({ tags: ["pork", "spicy"], max: limit(3) });render({ tags: ["pork", "spicy"], max: limit(3) });Mangled arguments
Arguments replaced by junk across the codebase. ci( and type them back.
The first exercise of every lesson is free. Pro unlocks the rest, every track, for good.
See ProThis exercise needs a keyboard. Read on, and play it at your computer.
Which layer?
Three short changes; the work is picking the layer. render gets item instead of the whole object it's handed, the tag list goes empty, and deliveryFee gets just zone. Each time, look at where the cursor landed and count the wrappings out from there.
Stuck? Show a hint
The first exercise of every lesson is free. Pro unlocks the rest, every track, for good.
See ProThis exercise needs a keyboard. Read on, and play it at your computer.
An empty box keeps its shape
Braces on their own lines work too. With the cursor anywhere in a function's body, di{ deletes every line between the braces and leaves the braces where they were: an empty box, ready for new contents. ci{ does the same and starts Insert mode on the empty line.
export function legacyTotal(order: Order) { const sum = order.items.reduce((s, i) => s + i.price, 0); return Math.round(sum * 1.07 * 100) / 100;}export function legacyTotal(order: Order) {}Empty the boxes
The old defaults object and the old total function both get new contents tomorrow. Empty each box with di{, from any line inside it.
The first exercise of every lesson is free. Pro unlocks the rest, every track, for good.
See ProThis exercise needs a keyboard. Read on, and play it at your computer.
Brackets, from memory
Press the keys. The cursor is between the brackets.
Arguments and strings
Ten wrong arguments and strings across the codebase, with the cursor wherever the last fix left it.
The first exercise of every lesson is free. Pro unlocks the rest, every track, for good.
See ProThis exercise needs a keyboard. Read on, and play it at your computer.
The quote function
2:45. quote must work before the 3 p.m. deploy. Top to bottom: the filter's argument is inStock now, the tag list goes empty, withTax takes order, zone now (everything it gets, subtotal(items), zone.tax, goes), and legacyQuote's body goes. Name each layer.
Stuck? Show a hint
The first exercise of every lesson is free. Pro unlocks the rest, every track, for good.
See ProThis exercise needs a keyboard. Read on, and play it at your computer.
Extra practice optional
The chapter is done. These rounds keep going as long as you like: stop whenever it stops being useful.
Brackets, endlessly
Endless mangled arguments and strings.
The first exercise of every lesson is free. Pro unlocks the rest, every track, for good.
See ProThis exercise needs a keyboard. Read on, and play it at your computer.