JavaScript, inside out · Chapter 20 of 51
Scope and hoisting
By the end of this chapter you can tell which variable a name means on any line, and whether it exists yet when that line runs. 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
- 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
The monsoon is here, and Meera added a ₹20 rain surcharge to deliveries, but it never reaches the bill. Anjali has noticed that once one item on the menu has an offer, every item after it says "(offer)" too. Ravi's shift report crashes before it starts, and his rush counter stops with an error. Kabir reads all four functions and says: "The logic is right. The variables are in the wrong place."
Chapter 2 showed one block in a file. Tapri's code has blocks inside functions, and names declared twice at different levels. Without knowing which variable a name finds, you can stare at a correct line that adds the surcharge to a variable nobody ever reads.
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
monsoonTotal: fix the surcharge that never reaches the bill
monsoonTotal(prices, raining) is broken now: the ₹20 surcharge never gets added. Fix it so it returns the total of the prices, plus 20 when raining is true: monsoonTotal([40, 35], true) gives 95, and monsoonTotal([40, 35], false) gives 75.
With no prices, a rainy day still costs the surcharge: monsoonTotal([], true) gives 20, and monsoonTotal([], false) gives 0.
- Task 2
priceTags: stop the offer tag leaking onto later items
priceTags(items) is broken now: after the first item with an offer, every later item gets (offer) too. Fix it so it returns one tag per item, in order, written like bun maska ₹35 (the name, a space, then ₹ and the price), with a space and (offer) added only for an item whose offer is true: masala chai ₹40 (offer).
An item with offer false, or with no offer at all, gets no (offer), wherever it comes in the list. An empty list gives an empty array.
- Task 3
shiftReport: fix the report that crashes before it starts
shiftReport(orders) is broken now: it throws a ReferenceError every time. Fix it so it returns the summary of the shift, the number of orders and the total of their totals, written like Shift: 3 orders, ₹105. With no orders it gives Shift: 0 orders, ₹0.
Keep the helper where it is, below the return line.
- Task 4
countRush: fix the counter that was never declared
countRush(orders) is broken now: it throws a ReferenceError. Fix it so it returns how many orders have hour 16 (the 4 pm rush): countRush([{ hour: 16 }, { hour: 9 }, { hour: 16 }]) gives 2, and an empty list gives 0.
The count must live inside the function: the check fails if a global variable called rushCount exists afterwards.
- Task 5 · interview question
Interview: hoistingPrediction, what the hoisting snippet prints
Export hoistingPrediction, an array of strings: one string for every line the snippet in the comment above it prints, in the order they are printed. Each printed value is turned into text with String, so a number 7 is written '7' and undefined is written 'undefined'. When the snippet catches an error, it prints the error's name.
The check runs the same snippet and compares.
What you will learn
- Scope and lookup: where a name can be seen, and how JavaScript looks outwards to find it.
- Shadowing and undeclared names: an inner name that hides an outer one, and a name that was never declared at all.
- var in a function: one variable for the whole function, not a new one each round.
- Hoisting inside functions: what a name holds before its line runs.
More in The order flow
Build "Scope and hoisting" 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.