← Lua Academy

Thinking in Lua · 5 / 69

Zero is true

YOUR TASK

Make this happen

3 steps
  1. Only false and nil are falsy in Lua. Zero and the empty string are truthy. Predict the result before changing the expression.

  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.isTruthy = function(value)
  return false
end

lesson.test.lua

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

t.it("zero is truthy", function()
  local s = subject.load()
  t.expect(s.isTruthy(0)):eq(true)
end)

t.it("false stays false", function()
  local s = subject.load()
  t.expect(s.isTruthy(false)):eq(false)
end)

t.it("nil is false", function()
  local s = subject.load()
  t.expect(s.isTruthy(nil)):eq(false)
end)

t.it("empty text is truthy", function()
  local s = subject.load()
  t.expect(s.isTruthy("")):eq(true)
end)