JavaScript, inside out · Chapter 6 of 51
Template literals
By the end of this chapter you can build any message out of values: names, sums, choices and several lines, in one piece of text. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: First day at Tapri
- 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 first "your order is ready" SMS went out to forty customers this morning, and every one of them read the placeholders instead of their own name. Anjali, who writes every word the customers see, also wants an order line that works out its own total, a greeting that changes with the time of day, and a receipt with the stall's name, the order and the total, each on its own line.
With what you know from the last chapter, you would glue the message together with +: 'Hi ' + name + ', your ' + item + ' is ready!'. That is four pieces and three plus signs for one short sentence, and dropping one of the spaces inside the quotes gives "Hi Anjali,your". A receipt of four lines would be twice as long, with no way yet to start a new line.
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
readyMessage: fix the SMS that prints its placeholders
Fix readyMessage(name, item). It must return Hi, the name, a comma, your, the item and is ready!, like readyMessage('Anjali', 'masala chai') gives 'Hi Anjali, your masala chai is ready!'.
It is broken now: it returns the placeholders themselves, with the dollar signs and braces, instead of the name and the item.
- Task 2
orderLine: one order line with its total
Write orderLine(item, qty, price). Return the qty, then x with a space on each side, the item, then = with a space on each side, and ₹ straight before the line total, which is qty times price.
orderLine('Masala chai', 2, 40) gives '2 x Masala chai = ₹80', and orderLine('Bun maska', 1, 35) gives '1 x Bun maska = ₹35'.
- Task 3
greeting: good morning or good evening
Write greeting(name, hour). The hour is a whole number from 0 to 23. Before 12 (0 to 11) return Good morning, a comma and a space, the name and an exclamation mark: greeting('Meera', 9) gives 'Good morning, Meera!'.
From 12 on, return Good evening the same way: greeting('Ravi', 16) gives 'Good evening, Ravi!'. Midnight, hour 0, is morning, and hour 12 is already evening.
- Task 4
receipt: a four-line receipt
Write receipt(order). The order has an id, a customer, an item, a qty and a price (of one, in rupees). Return one string of exactly four lines, separated by line breaks.
Line 1 is always Tapri, Indiranagar. Line 2 is Order #, the id, for and the customer: Order #42 for Ravi. Line 3 is the qty, x, the item, @ and ₹ with the price: 2 x Masala chai @ ₹40. Line 4 is Total: and ₹ with qty times price: Total: ₹80.
No line starts with a space, and there is no line break after the last line.
- Task 5
templatePredictions: what each template really gives
Export templatePredictions, an object with the keys a, b, c, d, e and f. For each key, write the whole text that the template with that letter in the comment above it gives, as a string, label included.
The check works out the same six templates and compares.
What you will learn
- Backticks and ${}: drop a value straight into the text.
- Any expression inside: a sum, or a choice with the conditional operator ? :.
- Several lines: the line breaks you type are part of the text.
- Values become text: whatever goes into ${} comes out as a string.
More in First day at Tapri
Build "Template literals" 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.