← Lua Academy

Working with data · 19 / 69

Measure text

YOUR TASK

Make this happen

3 steps
  1. Use the supported string library to measure a simple ASCII identifier. This example deliberately avoids Unicode boundary assumptions: byte length and user-perceived characters are different concepts.

  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.length = function(value: string): number
  return 0
end

lesson.test.lua

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

t.it("identifier length", function()
  local s = subject.load()
  t.expect(s.length("paper")):eq(5)
end)

t.it("empty text", function()
  local s = subject.load()
  t.expect(s.length("")):eq(0)
end)