← Lua Academy

Thinking in Lua · 7 / 69

Count inclusively

YOUR TASK

Make this happen

3 steps
  1. A numeric for loop includes its end value. Compute the sum from 1 through n, including n. For n = 0 the loop performs no iterations.

  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.triangle = function(n: number): number
  return n
end

lesson.test.lua

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

t.it("one through four", function()
  local s = subject.load()
  t.expect(s.triangle(4)):eq(10)
end)

t.it("no terms", function()
  local s = subject.load()
  t.expect(s.triangle(0)):eq(0)
end)