Functions and modules · 24 / 69
Capture a multiplier
YOUR TASK
Make this happen
A closure retains access to variables in its surrounding scope. Return a function that multiplies its argument by the captured factor.
Read the cases in
lesson.test.lua, then choose Check solution to see what needs to change.Update
subject.luauntil every test passes. Keep the supplied tests unchanged.
Read the starter and tests
subject.lua
exports.multiplier = function(factor: number): (number) -> number
return function(value: number): number
return value
end
end
lesson.test.lua
local t = require("test")
local subject = require("cococo.subject")
t.it("captures factor", function()
local s = subject.load()
local triple = s.multiplier(3)
t.expect(triple(4)):eq(12)
end)
t.it("independent closures", function()
local s = subject.load()
local twice = s.multiplier(2)
local five = s.multiplier(5)
t.expect(twice(3)):eq(6)
t.expect(five(3)):eq(15)
end)