Recap - Scope Puzzle
Part of the Fundamentals section of Coddy's Lua journey — lesson 87 of 90.
Challenge
EasyAnalyze 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 firstprintstatement will outputsecondOutput- what the secondprintstatement will outputthirdOutput- what the thirdprintstatement 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
4Operators 2 Relational & Logic
Equality OperatorsRelational OperatorsThe 'and' OperatorThe 'or' OperatorThe 'not' OperatorShort-Circuit EvaluationTruthy and Falsy ValuesRecap - Simple Logic7Basic Conditional Logic
The if-then StatementThe if-then-else StatementThe elseif StatementRecap - Treasure Chest2Variables and Data Types
What is a Variable?NumbersStringsBooleansThe Value 'nil'The type() FunctionNaming ConventionsRecap - Character Profile5Basic Output
Printing LiteralsPrinting VariablesPrinting Multiple ValuesCombining Strings & VariablesThe tostring() FunctionInputCastRecap - Status ReportRecap - Till 1208String Manipulation Basics
string.len()string.upper & string.lowerstring.sub()string.rep()string.find()Recap - Format Username11Tables 2: Dictionary-Style
Key-Value PairsCreating Dictionary-Style TablAccessing - Bracket NotationAccessing with Dot NotationAdding and Modifying PairsRemoving Pairs with nilRecap - Item Properties14Variable Scope
Global VariablesLocal VariablesWhy Use Local Variables?Scope and BlocksRecap - Scope Puzzle3Operators 1 Arithmetic & Conc
Arithmetic OperatorsModulo OperatorExponentiation OperatorString ConcatenationOperator PrecedenceRecap - Simple Calculations6Project: Character Stats Disp
Welcome MessageDeclare Character Stats9Functions Basics
Declaring a FunctionCalling a FunctionFunctions with ParametersFunctions with Multiple ParamsThe 'return' StatementRecap - Area Calculator