JavaScript, inside out · Chapter 18 of 51

Loops

By the end of this chapter you can walk through a list, an object or a number of tries one round at a time, skip a round, and stop the moment you have your answer. 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

Ravi runs the 4 pm rush with one pot of milk. He wants kitchen tickets for every order that is not cancelled, and the pot served first come, first served, stopping at the first order it cannot fill. He wants the stock items running low, a UPI payment retried a few times when the bank is slow, and, for a customer with a voucher, two items that use it up exactly.

The array methods from chapter 11 visit every item, every time. filter cannot stop at the first order that does not fit, so the pot would serve a small order from behind the queue. A retry has no array at all, just "try again until it works or we give up". With a loop, you decide when each round happens and when to stop.

What you build: 5 tasks

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

  1. Task 1

    kitchenTickets: a ticket for every order not cancelled

    Write kitchenTickets(orders). Each order has a token, an item and a qty, and maybe cancelled. Return a new array with one ticket per order, in the same order, written like #12 masala chai x2: a hash and the token, a space, the item, a space, then x and the qty.

    Skip the orders whose cancelled is true. An order with cancelled false, or with no cancelled at all, gets a ticket. An empty list, or a list where every order is cancelled, gives an empty array.

  2. Task 2

    serveFromPot: serve until the next order does not fit

    Write serveFromPot(milk, orders). milk is how many ml are in the pot, and each order needs ml of milk. Serve the orders first come, first served, and stop at the first order that needs more than is left: nobody behind it is served either, even a smaller order. Return an object with two keys: served (how many orders were served) and left (the ml still in the pot).

    Exactly enough is enough: 300 ml serves two orders of 150 and leaves 0. With no orders, served is 0 and left is all the milk.

  3. Task 3

    lowStock: the stall's own items running low

    Write lowStock(stock, limit). stock is an object of item names and how many are left. Return an array of the names whose count is below limit, in the order the keys appear: lowStock({ ginger: 4, milk: 30, sugar: 2 }, 10) gives ['ginger', 'sugar'].

    Exactly the limit is not low, and 0 is low. Only the object's own keys count: a key it inherits from another object is left out, even when its count is low.

  4. Task 4

    retryPayment: try a UPI payment, at least once

    Write retryPayment(tryPay, maxTries). tryPay is a function that tries the payment and returns true when it went through, false when it did not. Call it until it returns true or it has been called maxTries times, and return an object with two keys: paid (what the last try returned) and tries (how many times tryPay was called).

    Always try at least once, even when maxTries is 0: then the answer is paid false and tries 1 for a payment that fails. Never call tryPay again after it returns true.

  5. Task 5 · interview question

    Interview: voucherPair, two items that use up a voucher

    Write voucherPair(prices, voucher). Find two different items whose prices add up to exactly voucher, and return their positions as [i, j] with i smaller than j: voucherPair([40, 25, 35, 10], 60) gives [1, 2].

    Return the first such pair, scanning i from the start and then j after it: voucherPair([40, 25, 35, 10], 50) gives [0, 3]. The same item cannot be picked twice, so voucherPair([20, 35, 45], 40) gives null, but two different items with the same price can: voucherPair([20, 35, 20], 40) gives [0, 2]. With no pair, an empty list or only one item, return null.

What you will learn

  • for, while and do...while: repeat with a counter, until a condition fails, or at least once.
  • for...of and for...in: walk the values of an array, or the keys of an object (and its inherited keys).
  • continue and break: skip one round, or leave the loop.
  • Nested loops and labels: a loop inside a loop, and leaving both.

More in The order flow

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