JS essentials · build and read a to-do app
Topic Slides
Agenda Slides Open fullscreen ↗
Post-Class Notes
TL;DR
Today we built a to-do app with one AI prompt, then did the real work: reading every
line AI wrote and cleaning it up. We moved the JavaScript out of the HTML file into its
own todo/script.js, the same way we already keep CSS in its own file. Then we named the
pieces underneath the code, starting with variables (const and let). The one habit to
keep: AI can write the app in seconds, so your job is to read it, organize it, and make it
yours. Before Wednesday, reread your todo/script.js line by line and keep going on MP2.
What we did today
- Two lightning talks on AI study and dictation tools.
- A quick three-question review of last class on the DOM and events (answers below).
- Built a to-do app from one prompt, read the code, and split the JavaScript into its own file.
- Compared apps in groups: same prompt, different results.
- Started JavaScript essentials with variables.
The loop we are practicing
Every step today was one loop you can reuse on any project:
- Ask AI to build it. One clear prompt, one working app.
- Read every line. Find the moving parts and say what each one does.
- Clean it up. Split the code into separate files so it stays readable as it grows.
- Name the pieces. Learn the building blocks (variables, functions, conditions) on the code you already have in front of you.
- Make it yours. Change it by hand first, then ask AI for bigger changes and read what it adds.
Reading AI's code (READ then FIND)
When AI hands you a working app, open it and find these before you change anything:
- The
<script>tag. Is the JavaScript inside the HTML file or in its own file? - Every
querySelectorandgetElementById. What does each one select on the page? - Every
addEventListener. What event is each one waiting for (a click, a form submit, a checkbox change)? - Where the code changes the page. Look for
textContent,innerHTML,classList,createElement, and similar. - The words you do not know yet (
let,const,function). Guess what they do from how they are used. We name them next.
A good first move is to test it like the owner: open it in Live Server, click through it, and try the Enter key. You are responsible for knowing it works.
🤖 AI tip: Format the code before you read it (Mac: Shift+Option+F, Windows: Shift+Alt+F). JavaScript marks its structure with brackets, so a formatted file is far easier to follow than a wall of code.
Splitting the JavaScript out
Most AI to-do apps arrive as one HTML file with all the JavaScript jammed inside a
<script> block. We clean that up the same way we already separate CSS:
- Make a new file:
todo/script.js. - Move everything from inside
<script>...</script>intotodo/script.js. - Link it at the end of the
<body>:
<script src="script.js"></script>- Refresh. The app still works, and now
index.htmlholds the structure whilescript.jsholds the behavior.
Two things that came up live while we did this:
- Same folder means a bare filename. Because
script.jsandstyle.csssit in the sametodo/folder asindex.html, you link them asscript.jsandstyle.css, with notodo/in front. - Names have to match exactly. Our page lost all its styling because the file was named
style.cssbut the link pointed atstyles.css. One letter. When something silently does nothing, check your file and variable names first.
Variables: the first building block
The JavaScript file starts by creating variables, so that is where we started naming things.
- Declare with
constorlet. Useconstwhen the value will not be reassigned (most of the element handles in the to-do app). Useletwhen it will change (the list of todos, the current filter). - The
=sign means assignment, not equality. Read it as "put the value on the right into the name on the left." - Do not use
var. It is the old way and causes subtle bugs. You will still see it in old code and older AI output, so when you do, read it asletorconst. constis not fully frozen. For a list or an object, you can still change what is inside it. What you cannot do is reassign the whole variable to a new value.
We saw why this matters in the code you already have: the variable that holds your tasks is the app's memory, the function that runs on Add is the action, and the check for an empty box is the decision. Almost every app is that same trio: a variable holding state, a function reacting to an event, and a condition choosing what happens next. We cover functions and conditions on Wednesday.
Want to read ahead? The JavaScript essentials slides cover all of it, and the MDN guide to variables is a solid reference.
Quick review answers (DOM and events)
- A script in the page
<head>runsquerySelector('#go')and the console showsnull. The script runs before the body loads, so the button does not exist yet. Move the<script>to the end of the<body>. (deferfixes this too, but only for an external script file, not for code written straight into the page.) getElementById('#title')returnsnulleven though the id exists. Drop the#.getElementByIdwants the bare id (title). The#belongs withquerySelector.querySelector('.btn')only turns the first of three buttons red.querySelectorreturns the first match. To reach all of them you usequerySelectorAlland loop over the list, which we get to with arrays on Wednesday.
To do before Wednesday (6/24)
- Push your to-do app. Commit
todo/index.htmlandtodo/script.jsto youroim3690repo. This split structure is a CP3 item. - Reread your
todo/script.jsline by line. Can you explain every line? Mark the ones you cannot, and bring them Wednesday. - Update
logs/wk06.md. Add what you found reading the code and what your group compared: one feature another app had that yours did not, and one thing AI did differently in someone else's code. - Keep building MP2. The final version is due Sunday, 6/28, and CP3 is also due Sunday, 6/28.
- Read ahead through the JavaScript essentials slides so we can move faster Wednesday.
Wednesday we finish JavaScript essentials (functions and conditions), then move into arrays and loops, pulling live data from an API, and the MP3 launch.