← Lua Academy

Capstones · 67 / 69

Build an order summary

YOUR TASK

Make this happen

3 steps
  1. Combine records, arrays, loops and branches. Export summarize(lines), returning quantity and total. Each line has quantity and unitPrice. An empty order has zero quantity and zero total. Keep this calculation pure: it needs no platform calls.

  2. Read the cases in lesson.test.lua, then choose Check solution to see what needs to change.

  3. Update subject.lua until every test passes. Keep the supplied tests unchanged.

Read the starter and tests

subject.lua

type Line = { quantity: number, unitPrice: number }
exports.summarize = function(lines: { Line })
  return { quantity = 0, total = 0 }
end

lesson.test.lua

local t = require("test")
local subject = require("cococo.subject")

t.it("summarizes multiple lines", function()
  local s = subject.load()
  t.expect(s.summarize({
    { quantity = 2, unitPrice = 10 },
    { quantity = 3, unitPrice = 4 },
  })):deep_eq({ quantity = 5, total = 32 })
end)

t.it("empty order", function()
  local s = subject.load()
  t.expect(s.summarize({})):deep_eq({ quantity = 0, total = 0 })
end)