At the end of the last video, Jonas checked in and then left early. Sam checked in after him, and the Console said Arrived so far: 4 while only three people were in the room. The arrival bar stayed stuck at half. Removing Jonas’s row changed the page, but nothing else noticed.
In this lesson, you’ll find out why the count drifted, and you’ll fix it at the root. You’ll keep every fact about the guests in players and write one function that draws the list, a new “Arrived” line, and the bar from it, every time the data changes. Start by looking at where Priya’s page keeps its count.
Find the Copies of the Truth
The arrival count went wrong because Priya’s code keeps the same facts in more than one place. To see them, rerun the last video’s sequence on the code you have now. Save, reload, then click Check in Wei, Check in Jonas, Remove Jonas, and Check in Sam. Below section 1’s six load lines, the clicks log these:
Checked in: wei
Arrived so far: 2
Checked in: jonas
Arrived so far: 3
Checked in: sam
Arrived so far: 4
Three people are here: Wei, Lin, and Sam. So which part of the code believes there are four? Look at each place that holds a fact about who’s arrived:
players, filled once at load. No click ever changes it. Jonas is still in it, and nobody in it has arrived.let arrived, a running total. Each Check in click adds the button’sdata-partyto it, but Remove takes nothing away.- The page’s green rows, which
showArrivals()counts withli.arrived, then divides byplayers.length. Jonas’s green row left, Sam’s arrived, and four records are still inplayers, so the bar stays at two of four.
Each copy was right when a click last updated it. The Remove click updated only the page.
You could patch Remove to subtract Jonas’s party from arrived when his row is green. However, every new action, such as an undo, would need its own patch. Each patch would read facts back off the page, where they live as classes and text for people, not as data.
Instead, keep each fact in one place, called the app’s single source of truth. For Priya’s list, that’s players. Clicks change players, and your code draws the page again from it. Nothing reads the count back from the page.
Draw the Page From the Data
To make players the single source of truth, each record needs to hold whether that guest has arrived, and the page needs one function that draws everything from the records. First, give the count its own line. In scratch/index.html, add a paragraph right before the arrival bar’s div: