Menu
Coddy logo textTech

Recap - Scope Puzzle

Part of the Fundamentals section of Coddy's Lua journey — lesson 87 of 90.

challenge icon

Challenge

Easy

Analyze the following code snippet and predict what will be printed by each print statement. Create variables to store your predictions and then print them in the correct order.

Here is the code to analyze:

gameScore = 100

function updateScore()
    local gameScore = 250
    print(gameScore)
end

do
    local gameScore = 75
    updateScore()
    print(gameScore)
end

print(gameScore)

Create three variables to store your predictions:

  • firstOutput - what the first print statement will output
  • secondOutput - what the second print statement will output
  • thirdOutput - what the third print statement will output

Then print your three predictions in order, each on a separate line.

Try it yourself

-- Create variables to store your predictions
local firstOutput = -- TODO: Analyze the first print statement and assign the predicted value
local secondOutput = -- TODO: Analyze the second print statement and assign the predicted value
local thirdOutput = -- TODO: Analyze the third print statement and assign the predicted value

-- Print your predictions in order
print(firstOutput)
print(secondOutput)
print(thirdOutput)

All lessons in Fundamentals