Four programs with functions in them, each with exactly one thing wrong. Two crash, two do not, and in three of the four the error message names a line that is not where the mistake is.
That gap — between where a program notices and where it went wrong — is most of what makes debugging a skill rather than a lookup.
This prints 10 and then stops with an error about arithmetic on a nil value.
local function double(n) print(n * 2)end
local answer = double(5)
print("Answer plus one: " .. answer + 1)The player loses a life. It prints Lives left: 3.
local lives = 3
local function loseALife(lives) lives = lives - 1 return livesend
loseALife(lives)
print("Lives left: " .. lives)The function is correct. Something around it is not.
This should add up the table and print Total: 20. It prints Total: 4.
local function total(t) local sum = 0
for i, v in ipairs(t) do sum = sum + v return sum endend
print("Total: " .. total({4, 6, 10}))This stops immediately with attempt to call global 'greet' (a nil value).
print(greet("Ada"))
local function greet(name) return "Hello " .. nameendFor each program, give:
Bug 2 is the one to spend time on. There is nothing wrong inside the function, and writing down exactly what happened to the value is worth more than the fix.
Worth 5 points, graded on completion.