Menu
Coddy logo textTech

Recap - High Score Board

Part of the Logic & Flow section of Coddy's Lua journey — lesson 9 of 54.

challenge icon

Challenge

Easy

Write 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 numbers
  • newScore (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
end

All lessons in Logic & Flow