JavaScript, inside out · Chapter 17 of 51

Conditionals

By the end of this chapter you can make the code choose: one answer out of many, checked in the right order, with the edges where you want them. 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

Kabir has joined Tapri as senior developer, and his first job is the rules. Meera wants a price for any item typed at the counter, and nothing for a typo. Ravi charges delivery by distance in three bands and refuses orders after closing. Anjali wants the "your chai is ready" message to cope with a missing name and a customer who takes no sugar. And during the 4 pm rush, some token numbers win a Parle-G.

So far you have chosen between two values with a ternary and picked a fallback with ||. Ravi's fees have four answers, so a ternary would sit inside a ternary inside a ternary, and one misplaced < charges the wrong band without any error. And || treats a sugar of 0 as missing, so "no sugar" quietly becomes two spoons.

What you build: 5 tasks

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

  1. Task 1

    menuPrice: a menu price by name, null for anything else

    Write menuPrice(item). Return the price in rupees of the item named: masala chai 40, cutting chai 25, ginger chai 35, bun maska 35, parle-g 10. The names are lower case, exactly as written here.

    Anything else returns null: menuPrice('coffee') and menuPrice('') give null, and so does a name in other capitals, like menuPrice('Masala Chai').

  2. Task 2

    deliveryFee: a fee for any distance

    Write deliveryFee(km). Return the delivery fee in rupees for a distance in km: up to and including 1 km is free (0), up to and including 3 km costs 20, up to and including 6 km costs 40, and more than 6 km returns null, because Tapri does not deliver that far.

    The edges are included in the cheaper band: 1 gives 0, 3 gives 20 and 6 gives 40, while 1.1 gives 20, 3.2 gives 40 and 6.01 gives null. A distance of 0 gives 0.

  3. Task 3

    checkOrder: the first problem with an order

    Write checkOrder(order, hour). The order has items (an array) and payment (a string), and hour is a whole number from 0 to 23. Return the first problem found, checking in exactly this order:

    'closed' when the hour is before 7 or is 23 (Tapri is open from 7:00 to 22:59, so 7 and 22 are open); 'empty' when the order has no items; 'unpaid' when the payment is not 'upi' and not 'cash', including when there is no payment at all; and 'ok' when nothing is wrong.

    With several problems at once, only the first in that list is returned: an empty order paid by card at hour 23 is 'closed', and at hour 12 it is 'empty'.

  4. Task 4

    readyMessage: tell the customer their chai is ready

    Write readyMessage(order). The order has name, cups and sugar. Return the text Anjali, your chai is ready (sugar: 0) for one cup, and Guest, your 3 chais are ready (sugar: 2) for more than one: the name, a comma, then your chai is for 1 cup or your N chais are for any other number, then ready and the sugar in brackets.

    A missing or empty name becomes Guest. A missing or null sugar becomes 2, but a sugar of 0 is a real choice and stays 0.

  5. Task 5 · interview question

    Interview: tokenReward, the rush hour token rewards

    Write tokenReward(token). token is a positive whole number. Return 'Parle-G and kulhad' when it is a multiple of both 3 and 5, 'Parle-G' when it is a multiple of 3, 'kulhad' when it is a multiple of 5, and '' (an empty string) for anything else.

    So 9 gives 'Parle-G', 10 gives 'kulhad', 15 and 30 give 'Parle-G and kulhad', and 1 and 31 give ''.

What you will learn

  • if, else if, else: test in order, and the first true test wins.
  • switch: compare one value with many, and the missing break.
  • Guard clauses: check each problem first and leave early.
  • ??: a fallback only for a value that is not there.

More in The order flow

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