Skip to content

Lab · JavaScript Programming (language in depth)

Lab: the stock list of a kirana shop

Labbeginner· about 1.5 hours· 5 steps

The brief

Gupta General Store in Indore keeps its stock as a list of objects. Write the functions its billing app needs: bill lines with map, a low-stock list with filter, stock value and totals per category with reduce, restocking without changing the old list, and a safe deep copy of an order.

Why it matters

Almost all app data is lists of objects: products, orders, messages. map, filter and reduce are how professionals work with them, and "why did changing this copy also change the original?" is one of the most common bugs (and interview questions) in JavaScript.

Skills you practise

  • map, filter and reduce
  • destructuring in parameters
  • objects as counters
  • spread to copy and change
  • references versus copies
  • structuredClone

What you will have at the end

stock.js: tested list functions for a shop's billing app, with no accidental changes to shared data.

How it is marked (rubric, 100 points)
Correct resultsEvery check passes.50
Array methods used wellmap, filter and reduce instead of hand-written loops, with start values for reduce.25
No side effectsFunctions return new data and never change their inputs.15
Can explainYou can explain the difference between a shallow and a deep copy.10

0/5 steps done

Step 1 of 5

Bill lines with map

billLines(cart) turns [{ name, price, qty }] into lines like "Toor dal 1 kg × 2 = ₹324", one per item.

map makes a new array of the same length by running a function on each item. Destructuring in the arrow's parameter, ({ name, price, qty }) => …, pulls the fields out so the template literal stays short.

Run shows what your code prints. Check runs the step's tests. Ctrl+Enter checks too.