Tutorial
Display several tasks
Use a list and a loop to number your tasks.
Start with chapter 1's tasks.tea. We're going to replace the single title with a list and give each task a number.
Put the titles in a list
Replace the title binding with:
const tasks = ["Learn Tea", "Build a CLI", "Take a break"]Square brackets create a list. Since each element is a string, Tea infers List[String]. A list keeps its elements in order; the first item has index 0 when you access it directly.
Print each title
Replace the old output statement with:
var number = 1
for title in tasks
@println(`${number}. ${title}`)
number = number + 1
endfor title in tasks binds each element to title in turn. The block closes with end. Indent the body by two spaces for readability; it is the end keyword that marks the block's boundary.
We use var for number because it changes on each iteration. Its initial value is an Int. The counter starts at 1 because these numbers are for the person using the tool, even though list indexes start at 0.
Interpolation accepts numbers as well as strings, so there's no need to convert number before printing it.
Run the program
tea tasks.tea1. Learn Tea
2. Build a CLI
3. Take a breakComplete checkpoint
tasks.tea
const tasks = ["Learn Tea", "Build a CLI", "Take a break"]
var number = 1
for title in tasks
@println(`${number}. ${title}`)
number = number + 1
endTry it yourself
Add a fourth task and print the total number of tasks after the loop.
Hint
Add another string inside the list's square brackets. After end, use @len(tasks) to get the list length:
@println(`${@len(tasks)} tasks`)You now have a collection and a loop. Next, each task will carry its own completion status.
Read more: lists and inferred types, length and other built-ins.