← Lua Academy

Working with data · 17 / 69

Total named stock

YOUR TASK

Make this happen

3 steps
  1. pairs visits key/value entries in a table, including string keys. Sum values without depending on the iteration order.

  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

exports.stock = function(items: { [string]: number }): number
  return 0
end

lesson.test.lua

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

t.it("named keys count", function()
  local s = subject.load()
  t.expect(s.stock({ paper = 12, ink = 3 })):eq(15)
end)

t.it("empty map", function()
  local s = subject.load()
  t.expect(s.stock({})):eq(0)
end)