← Lua Academy

Working with data · 14 / 69

Keep positive quantities

YOUR TASK

Make this happen

3 steps
  1. Build a new array instead of changing the input. table.insert appends one value. Preserve the order of all positive quantities.

  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.positive = function(values: { number }): { number }
  return values
end

lesson.test.lua

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

t.it("filters zero and negatives", function()
  local s = subject.load()
  t.expect(s.positive({ 2, 0, -1, 3 })):deep_eq({ 2, 3 })
end)

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