Back

Debug Challenge: Decisions

divider

Objective

Four short programs about decisions, each with exactly one thing wrong. All four run perfectly and all four are wrong.

Not one of them produces an error message. Two of them even produce output that looks reasonable until you check it against what the program was supposed to say.


The Four Programs


Bug 1

This should report a letter grade. A score of 95 prints D.

Bug 1
local score = 95
if score >= 60 then
print("D")
elseif score >= 70 then
print("C")
elseif score >= 80 then
print("B")
elseif score >= 90 then
print("A")
else
print("F")
end

Bug 2

This should describe the temperature in one word. At 85 it prints three lines.

Bug 2
local temp = 85
if temp > 80 then
print("Hot")
end
if temp > 60 then
print("Warm")
end
if temp > 40 then
print("Cool")
end

Bug 3

This should move only when the key is a or d. It prints moving for every key, including x.

Bug 3
local key = "x"
if key == "a" or "d" then
print("moving")
end

This one is worth more thought than the others. Nothing here is a typo.


Bug 4

An order of exactly $50 should get free shipping and nothing else. It prints the free shipping line and the missed-it line.

Bug 4
local total = 50
if total < 50 then
print("Shipping: 6.00")
else
print("Free shipping")
end
if total <= 50 then
print("You just missed free shipping!")
end

Submit

For each program, give:

  • One sentence on what was wrong
  • The corrected line, or lines, of code
  • For bug 3 only: what the condition is actually asking

Trace them on paper. Pick a value, follow it line by line, and write down what each condition evaluates to. That is the same skill the exam's trace questions want.

Worth 5 points, graded on completion.

Commence Debugging