← Lua Academy

Functions and modules · 23 / 69

Extract a calculation

YOUR TASK

Make this happen

3 steps
  1. A local helper can describe one reusable calculation. Export a function that adds the areas of two identical rectangles.

  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

local function area(width: number, height: number): number
  return width * height
end
exports.pairArea = function(w: number, h: number): number
  return area(w, h)
end

lesson.test.lua

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

t.it("two rectangles", function()
  local s = subject.load()
  t.expect(s.pairArea(3, 4)):eq(24)
end)