← Lua Academy

Functions and modules · 22 / 69

Return a range

YOUR TASK

Make this happen

3 steps
  1. Lua functions can return more than one value. The caller assigns those results to separate locals. Implement a function that returns the smaller number first.

  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.range = function(a: number, b: number): (number, number)
  return a, b
end

lesson.test.lua

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

t.it("sorts reversed inputs", function()
  local s = subject.load()
  local low, high = s.range(9, 2)
  t.expect(low):eq(2)
  t.expect(high):eq(9)
end)

t.it("keeps ordered inputs", function()
  local s = subject.load()
  local low, high = s.range(2, 9)
  t.expect(low):eq(2)
  t.expect(high):eq(9)
end)