In this lesson, you'll explore iterating through a list in two ways: using a foreach to text each number in a list, and using a counter to iterate through and play each note in a list of music.
Foreach demo: adding a list of numbers.
One of the classic beginning programming problems involves computing the sum of a list of numbers. Foreach is perfect for this.
Revisiting the "Love You" app
First, consider the foreach method of displaying a list in a label:
How many times would the label change?
How would you describe the variable "item"? What does it represent?
Write a LoveYou App that has a fixed list of three phone numbers. The app should have a single button that, when clicked, texts the three phone numbers a message. Use a foreach block to make this happen.
Revisiting the Music Player
Some issues with playing music:
Need to initialized the sound files in Screen.Initialize:
for-loop doesn't work to play a sequence
Playing back a sequence of notes using Clock Interval and Counter
The strategy for playing back notes is to use an index and increment it as you play each note, something like you did in the Quiz app.
Define a variable "index"
When the playback button is clicked, set index to 1 and enable clock.
In the clock.Timer, check if index is within range of the list. If it is, select the next note (select the indexth item), play it, and increment the index. If index is too big, disable clock as you're done.
Hint: within your clock timer, you'll want to have a row of blocks like the following: