Menu
CoddyTech

Recap: Arrays and Maps

Part of the Introduction To Luau section of Coddy's Lua journey. Lesson 22 of 80.

challenge icon

Challenge

Medium

Count each fruit and print the counts in alphabetical order.

  1. Loop over items with ipairs, counting into local counts: {[string]: number} = {}.
  2. Collect the keys into local names: {string} = {} and call table.sort(names).
  3. For each name in names, print `{name}: {counts[name]}`.

Press Run Code: the output shows apple: 3, banana: 2 and cherry: 1.

Try it yourself

local items: {string} = {"apple", "banana", "apple", "cherry", "banana", "apple"}

-- 1) count each item into counts: {[string]: number}
-- 2) collect the keys into names: {string} and sort them
-- 3) print `{name}: {count}` for each sorted name
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Introduction To Luau

Practice on your own: Online Luau compiler