← Data apps

Records and fields

Use the songs in a table: show the clicked song as Now playing, add up every song's length with For each row, and find the longest song by keeping the best one so far. Each row is a record, and LoopItem.length or ClickedItem.title reads one field of it.

A music app tells you the playlist's total time and its longest track. Somewhere, a loop goes through every song and keeps a running total.

A table is a list of records. Today you'll read the fields of one record:

Then you'll use two common patterns: a running total (add each song's length) and a best so far (keep the longest song seen).

The playlist app with a table of four songs, a Now playing line, and buttons Total length and Longest song with their answers beside them.

How this lesson works: See it, Build, Check, Make it yours
See itTry the finished thing first, so you know what you are building.

Try the finished app

This is the app you'll build, open on the Run tab. Click a song, then the two buttons. On the left, total and longest show their values now.

Try it yourselfYour edits are saved in this browser.

BuildWatch the short clip, then do the steps. Your app carries on from the step before.

Build 1: now playing

  1. Add an event: An item in SongTable is clicked.
  2. Add the action NowText, Set NowText text to "Now playing: " & ClickedItem.title & " by " & ClickedItem.artist
  3. Run it and click a few songs.
Try it yourselfYour edits are saved in this browser.

Clicking a song sets NowText to its title and artist.

    Run your program. This ticks when your code does all of the above.

    BuildWatch the short clip, then do the steps. Your app carries on from the step before.

    Build 2: a running total

    The variable total is made for you.

    1. Add an event: TotalButton is clicked → Set variable total to 0.
    2. Give it a sub-event (the event's ⋯ menu, Add a sub-event). Its condition: Tables, For each row in table Songs. Its action: Add LoopItem.length to variable total.
    3. Add a second sub-event below it, with no condition and the action Set TotalText text to "Total: " & total & " min". It runs after the loop has finished.
    4. Run it. Check the answer by adding up the lengths yourself.
    A running total · Sound on for the voiceover
    Try it yourselfYour edits are saved in this browser.

    Total length sets total to 0, adds LoopItem.length for each row, then shows the total.

      Run your program. This ticks when your code does all of the above.

      BuildWatch the short clip, then do the steps. Your app carries on from the step before.

      Build 3: the longest song

      A variable can hold a whole row. longest will hold the longest song found so far.

      1. Add an event: LongestButton is clicked → Set variable longest to item(Songs, 1) (the first song is the best so far, to begin with).
      2. Add a sub-event: For each row in table Songs, and a second condition Compare LoopItem.length > longest.length. Its action: Set variable longest to LoopItem.
      3. Add a sub-event with no condition: Set LongestText text to "Longest: " & longest.title
      4. Run it. Then add a song longer than all the others, and try again.
      Try it yourselfYour edits are saved in this browser.

      Longest song starts with row 1, swaps in any longer row inside For each row, then shows its title.

        Run your program. This ticks when your code does all of the above.

        CheckA quick check that the idea has stuck.

        Check: trace the loop

        Songs has lengths 3, 5, 4, 2 in that order. Halfway through the Total length event, after the second row, what is total?

        Nib's Total length always shows the last song's length. Click the line where Nib goes wrong.

        Make it yoursChange your app on purpose, then try it on someone.

        Make it yours: average and shortest

        count(Songs) is how many rows there are. Change TotalText's text to also show round(total / count(Songs)), such as "Total: " & total & " min, average " & round(total / count(Songs)).

        There's a Shortest song button, a ShortestText and a variable shortest. Build it like the longest song. Which one thing changes?

        Try it yourselfYour edits are saved in this browser.

        TotalText also shows the average length: total divided by the number of songs.

          Run your program. This ticks when your code does all of the above.

          Shortest song finds the row with the smallest length and shows its title.

            Run your program. This ticks when your code does all of the above.

            Now you can
            Next lesson4. Saving between visitsMake the playlist remember its songs when the app is closed and opened again: tick Remember between visits on the Songs table, test a return visit and a first visit, and greet the user differently on each. The songs are kept in this browser only.
            For teachers

            Students read fields of records: ClickedItem.title for a Now playing line, LoopItem.length inside For each row to total the lengths (an accumulator), and a variable holding a whole row to find the longest song (a running maximum). The Check traces the total and finds Nib resetting it inside the loop. Make it yours: the average with count(), and the shortest song. About 60 minutes.

            Open the teacher guide: answers, solutions, slides and a worksheet.

            Students will be able to

            Before class

            These are the first named algorithms of the course: the accumulator (running total) and the running maximum (best so far). Name them as algorithm ideas, not App Builder features: they come back in Python and spreadsheets.

            A variable can hold one whole row: Set variable longest to item(Songs, 1), then longest.title. While the app runs, the sidebar shows it as { title: ..., artist: ..., length: ... }.

            A sub-event with no condition always runs; put it after the loop to show the result.

            Running the lesson

            1. Before Build 2 (whole class): add up 3, 5, 4, 2 aloud, saying the running total after each. That's the loop.
            2. Build 3: one student reads the lengths aloud, another holds up the longest so far and swaps it when a longer one comes.
            3. Check: trace the total in a table on the board: row, length, total after.

            The hard part (on purpose)

            Where each part sits: start the total on the parent event, add inside the loop, show it in a sub-event after the loop. The order of sub-events matters, and the actions of an event run before its sub-events.

            When students get stuck

            Stretch and support

            Stretch: Show how many songs are longer than 3 minutes (a count with a condition).

            Support: Build the total together, saying each step aloud; pairs do the longest song.

            Where it leads

            Next: Saving between visits.

            What each step is for