JavaScript, inside out · Chapter 8 of 51
Truthy and falsy
By the end of this chapter you can tell which values count as true and which count as false in a condition, and use || and && for defaults without losing a real 0. 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
At 4 pm the bun maska runs out. Ravi types 0 into the stock screen, and the screen says "Not counted yet", so the counter keeps selling buns that do not exist. Anjali has two more: the checkout button is never greyed out, even with nothing in the cart, and customers who skip the name box are greeted as "Hi , welcome!". She also wants each customer's coupon shown in capitals, and nothing at all when there is none.
With what you know so far, you would test every empty case by hand: name === '' || name === undefined || name === null, and one more for each new way a value can be missing. Or you write the short version, a value on its own in a condition, without knowing what it does with 0 or an empty cart. That short version is where all three bugs come from.
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
displayName: Guest when the name is blank
Write displayName(name). Return the name when there is one, and 'Guest' when the name is empty text, undefined or null.
displayName('Anjali') gives 'Anjali', and displayName('') gives 'Guest'.
- Task 2
stockLabel: fix the label that treats 0 as not counted
Fix stockLabel(count). When count is null or undefined, return 'Not counted yet'. Otherwise return the count, a space and left: stockLabel(12) gives '12 left', and stockLabel(0) gives '0 left'.
It is broken now: a count of 0 shows 'Not counted yet'.
- Task 3
isEmptyCart: fix the check that never says empty
Fix isEmptyCart(cart). The cart is always an array of item names. Return true when it has no items, and false when it has one or more: isEmptyCart([]) gives true, and isEmptyCart(['masala chai']) gives false.
It is broken now: it never says true, not even for an empty cart.
- Task 4
couponCode: the coupon in capitals, or nothing
Write couponCode(customer). The customer is an object with a name and maybe a coupon. Return the coupon in capitals: couponCode({ name: 'Ravi', coupon: 'chai10' }) gives 'CHAI10'.
When there is no coupon (the coupon key is missing, or is empty text or null), return empty text, '', never undefined or null.
- Task 5 · interview question
Interview: truthyPredictions, which values are truthy
Export truthyPredictions, an array of twelve booleans: what Boolean gives for each value in the comment above it (true for a truthy value, false for a falsy one), in the same order.
The check runs Boolean on the same twelve values and compares.
What you will learn
- Truthy and falsy: the short list of values that count as false.
- || and &&: they hand back one of their two values, which makes defaults and guards short.
- The two traps: a falsy value that is a real answer, and an empty value that is not falsy.
More in First day at Tapri
Build "Truthy and falsy" 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.