Arrays and loops; debugging your MP2 code
Mini Project 2: Interactive Web Tool (repo URL) Due Wednesday, 10/21 Mini Project 2: Interactive Web Tool Due Thursday, 10/22 Checkpoint 4 Due Sunday, 10/25
Agenda Slides Open fullscreen β
Open slides ↗Post-Class Notes
TL;DR#
Today covered the Mini Project 2 showcase, a lightning talk on what makes an app succeed, seven review questions on conditionals, and the arrays and loops slides: an array, the for loop and its two shorter forms, and the patterns built on them, then the Playlist. The habit worth keeping: in any tool, find the array that holds the data and the loop that puts it on the page.
Before next class#
Checkpoint 4 closes Sunday 10/25.
- Mini Project 2. Your repo URL on Canvas by Wednesday 10/21, and the tool deployed and working by 3:00 PM Thursday 10/22.
- The Playlist in your
oim3690repo by Sunday 10/25:playlist.htmlandjs/playlist.js, following The Playlist below. Commit and push both files. Both are on Checkpoint 4. logs/wk08.mdon GitHub by Sunday 10/25, as the weekly notes guide describes. Open your repo on GitHub and check that it is there.
The Mini Project 2 showcase#
On Thursday 10/22, I will open each tool on the classroom screen and use it as a first-time user, while you tell the room from your seat what it does. A few of you will get one question about your JavaScript: I point at a line or a block and ask what the page will do if it is deleted or changed. You answer first, then we change it and reload.
- The code is read on my screen, so close your laptop for your question.
- You may bring notes.
- You may tell me how you would ask your agent about the code, and I will ask it.
- You can keep updating the project after you submit the URL. What I open on Thursday is what is deployed at 3:00 PM.
How many people use AI#
Andrej Karpathy posted a rough estimate on 10/11 of how many people have seen what current models can do. He calls it napkin math:
- About 6 billion people have barely come in contact with large language models.
- One to two billion use free chat products, mostly as a better search engine or a writing aid.
- About 20 million use frontier models for math and code, and see agents finish in a prompt projects that used to take weeks.
- About 5,000 have access to the labs' internal systems.
The gap between the second and third groups is where tools built with an agent can find users.
Which branch can never run#
function getDiscount(total, isMember) {
if (total >= 100 || isMember) {
return 10;
} else if (total >= 100 && isMember) {
return 20; // never reached
} else {
return 0;
}
}|| is the logical OR operator and && is logical AND. JavaScript evaluates || from the left and stops at the first true side, which is called short-circuit evaluation, and return ends the function. Any input that makes the && condition true has already made the || condition true, so the 20 branch can never run. letter(95) follows the same rule: the first true branch wins, so the narrower condition goes first.
Truthy and falsy values#
if (count) treats 0 and "" as false. Every other number is true, -1 included, so if (count) with count = -1 shows "You have items". trim() removes the spaces at both ends of a string, so a field holding only spaces becomes "", and if (text) adds nothing. The spaces inside a string stay.
How a for loop runs#
for (let i = 0; i < 100; i += 2) {
console.log(i); // 0, 2, 4, ... 98
}The first part runs once, at the start. The middle part is checked before every round, and the loop keeps going while it is true: with i > 100 in its place, nothing prints, because the first check is already false. The last part runs after each round. Start at 1 and the same loop prints the odd numbers.
For an array, for (const song of songs) and songs.forEach(...) do the same work with less to read, and they are what agent-written code mostly uses. Name the loop variable as the singular of the array: for (const price of prices).
Patterns built on a loop#
Most of what an app does with a list is a loop plus one small idea, and the slides have a playground for each:
- Filter: an
ifinside the loop keeps only the items that meet a condition. Search is a filter on a name or a value. - Accumulate: a running total that each round adds to.
- Count: a counter that goes up when an
ifmatches. - Turn a number into a display: a star rating built by repeating a character.
Sorting, which the room also named, puts the items in order before the loop shows them.
The Playlist#
The Playlist slide has the full exercise. The way we ran it in class:
- Ask your agent for
playlist.htmlwith a<ul id="playlist">andjs/playlist.js, holding an array of five song titles, with the loop left out - Write the
for...ofloop yourself: create an<li>for each song and append it to the list - Ask your agent to check your loop, then add an input and an Add song button
- In its version, find the array, the loop, and what runs when the button is clicked
An array of strings is enough to start. Each song can grow into an object later, with an artist, an album cover or an audio file.
Typing a star or an emoji#
The β
in the star rating is the Unicode character U+2605. To open the emoji picker, press Win+. (Windows) or Ctrl+Cmd+Space (macOS).