Recap - High Score Board
Part of the Logic & Flow section of Coddy's Lua journey — lesson 9 of 54.
Challenge
EasyWrite a function updateHighScores that takes a scores table and a newScore number, and returns the updated high score board containing only the top 5 scores in descending order.
Add the new score to the table, sort all scores from highest to lowest, and keep only the top 5 scores.
Logic:
- Use
table.insert()to add the new score to the scores table - Use
table.sort()with a custom comparison function to sort scores in descending order (highest first) - If the table has more than 5 scores, use
table.remove()to remove scores beyond the top 5 - Return the updated scores table
Parameters:
scores(table): A list-style table containing current high scores as numbersnewScore(number): The new score to add to the board
Returns: A table containing the top 5 scores sorted from highest to lowest (table)
Try it yourself
function updateHighScores(scores, newScore)
-- Write code here
endAll lessons in Logic & Flow
1Advanced Table Iteration
Iterating with pairs()Iterating with ipairs()pairs() vs. ipairs()Recap - Character Sheet2More Table Library Functions
table.concat()table construction & unpack()table.sort()Custom Sorting with FunctionsRecap - High Score Board