MP2 showcase and fetch; MP3 starts
Topic Slides
Agenda Slides Open fullscreen β
Open slides ↗Post-Class Notes
TL;DR#
Today the Mini Project 2 showcase finished, the walkthrough read two projects' code, and Mini Project 3 opened with what an API is. The habit worth keeping: to read code an agent wrote, start from what runs when the page opens, then follow the listener on the button you care about.
Before next class#
Checkpoint 5 is due Sunday 11/08.
- Mini Project 3. Create the new public repo, choose your API, and check it in the Console as the spec describes. Your repo URL is due on Canvas Wednesday 11/04, and the project is due deployed and working by 3:00 PM Thursday 11/05.
logs/wk09.mdon GitHub by Sunday 11/01, with three ideas for Mini Project 3, each written as "I wish I had a tool that looks up ___ and helps me ___." This note is on Checkpoint 5.
Thank you for your feedback. Every answer to the midterm survey is on Canvas: Midterm Feedback Survey Results.
Reading code an agent wrote#
- Find the entry point. Scroll to the bottom of the script. The call there, often
render()orrenderAll(), runs when the page opens: it loads the saved data and draws the page. - Find the code behind a button. Each button gets its code from an
addEventListenercall. Search the file for the event (submit,click) or for a word in the button's label, or ask your agent: Find the code that runs when I click Add to Plan. - Long code repeats one shape. A long submit handler reads its inputs, checks them with
if, adds an object to an array withpush, saves, and draws the page again. That is the to-do list from our first JavaScript class. Mathis part of JavaScript itself.Math.min,Math.maxandMath.roundwork in any JavaScript program, with nothing to define or load. The browser adds its own on top, such aslocalStorageandfetch. Reference: Math on MDN
Loading saved data safely#
One of the projects loads its saved entries like this:
let entries = [];
try {
const stored = JSON.parse(localStorage.getItem(STORAGE_KEY) || '[]');
entries = Array.isArray(stored) ? stored.filter(e => e && typeof e.date === 'string' && Number.isFinite(e.score)) : [];
} catch (_) { entries = []; }Each part handles a different case:
- Nothing saved yet:
getItemreturnsnull, and|| '[]'turns that into an empty list. - Saved, but not a list: the
Array.isArraycheck gives an empty list, and thefilterdrops entries without a date or a score. - Saved text that is not valid JSON, or storage the browser has blocked:
JSON.parseorlocalStoragethrows an error, andcatchsets an empty list.
Without try and catch, that error would stop the script, and nothing below it would run. Fetch will use the same pattern on Thursday 10/29, because a request can fail too.
Ideas from the showcase#
- Export needs no API. A button that downloads the data as CSV, or prints it, works with what is already in
localStorage. - Your user decides what counts as valid. A $0 budget line looks like a mistake, but some club events cost nothing and still have to be submitted. Write the rule in
PRD.mdbefore you ask the agent to refuse anything. - Keep the summary in view. When users scroll to find the totals, a second column or a fixed panel can hold them.
- Save what the user enters. A tool that loses its entries on a reload needs
localStorage. - Look at what you track in a spreadsheet. Many tools start as a replacement for one sheet.
- A tool that handles sensitive data has to earn trust. Say where the data goes, or keep it on the user's own computer.
What an API is#
An API gives your code data in a format it can read, usually JSON. Open CoinGecko's Bitcoin price in your browser and reload it a few times: the number changes with the price, and the response is a small JSON object. The course API's Massachusetts towns returns every town with its population, income per capita, house value and tax rate.
Your code sends a request to a URL and gets data back, the way you order from a waiter without seeing the kitchen. Start Mini Project 3 from your idea, then search for a free API that has the data. The spec lists APIs that need no key.
Where Mini Project 3 is deployed#
A project whose API needs no key deploys on GitHub Pages, as before. A project whose API needs a key deploys on Vercel, because anyone who opens a page's files can read a key written in them. Thursday 10/29 will cover how.
MCP and APIs#
An API is one service's own interface. You read its documentation, write code for its URLs and the JSON it returns, and your code decides when to call it.
MCP, the Model Context Protocol, is one standard way to describe tools to an AI agent. Each tool states its name, what it does and what input it takes; the agent reads that list and decides for itself when to call one. An MCP server often calls an ordinary API underneath. Reference: Introducing the Model Context Protocol
Mini Project 3 uses an API, called from your own code.