← Lua Academy

Working with data · 13 / 69

Start at one

YOUR TASK

Make this happen

3 steps
  1. The first element of a Lua array is at index 1. This exercise uses a guaranteed nonempty array; later lessons handle missing elements with optional types.

  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.first = function(values: { string }): string
  return values[2]
end

lesson.test.lua

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

t.it("first entry", function()
  local s = subject.load()
  t.expect(s.first({ "alpha", "beta" })):eq("alpha")
end)