Arrays and loops; debugging JavaScript

Thursday, 10/15 · Week 7

Post-Class Notes

TL;DR#

Today covered the new Mini Project 2 dates and showcase, ways to make your AI budget last, the course website's MCP server, four review questions, and the rest of the JavaScript essentials slides: return, the order code runs in, and conditionals. Arrays and debugging will start next Tuesday. The habit worth keeping: for any if in your code, be able to state what the page will do if that line is deleted.

Before next class#

Checkpoint 4 is open and closes Sunday 10/25.

  1. 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. Describe the four states in PRD.md (empty, invalid input, in use, after a reset), and write a short DESIGN.md with Design Direction before your agent styles the tool.
  2. Member Discount by Sunday 10/18. Create discount.html in your oim3690 repo, following the steps under Writing getDiscount yourself below, then commit and push it.
  3. logs/wk07.md on GitHub by Sunday 10/18, as the weekly notes guide describes. Open your repo on GitHub and check that it is there.

The Spending Tracker on the slides is not due this week. We will work on it in class next Tuesday.

The Mini Project 2 showcase#

On Thursday 10/22, I will open each tool on the classroom screen, and you will tell the room from your seat what it does and which interaction to try. A few of you, drawn at random, 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. You answer first, then we delete it and reload. Everyone is asked once over the semester, so prepare for every showcase.

Making your AI budget last#

Every message you send to an agent carries the whole conversation before it, so a long chat costs more with each message. To make the $100 last:

  • Start a new chat for each new task. When a chat gets long, ask your agent to write what matters to a file, then start a new chat from that file.
  • Put related requests in one message.
  • Make a one-line change yourself when you know where it goes.
  • Attach a screenshot only for a visual problem. For an error or some text, paste the text.

None of this is a reason to use AI less. The Babson key is for coursework only; its rules are in the Babson AI key guide.

Asking your agent about the course#

The course website runs a read-only MCP server that an agent can use to look up deadlines, assignments, sessions, guides and the syllabus. Its address is https://oim-mcp.zhili.dev/oim3690/mcp.

  • Claude Code: claude mcp add --transport http oim3690 https://oim-mcp.zhili.dev/oim3690/mcp
  • Claude.ai: add it as a custom connector with the same address.
  • GitHub Copilot in VS Code: put this in .vscode/mcp.json in the folder you have open:
{ "servers": { "oim3690": { "type": "http", "url": "https://oim-mcp.zhili.dev/oim3690/mcp" } } }

An agent that visits the site without it can start from llms.txt, which describes the site for AI tools. Compared with downloading a page with curl, which fetches one URL with all its HTML and CSS, the MCP server hands the agent the course content already structured.

What innerHTML does with user input#
li.innerHTML = input.value;    // reads the text as HTML
li.textContent = input.value;  // shows the text as typed

With innerHTML, a user who types <b>Milk</b> gets Milk in bold, and a user can also insert other HTML, such as a form or an image with an onerror attribute that runs JavaScript. Use textContent for anything a user types. list.innerHTML = "", which clears a list your own code builds, is fine.

Returning a value#

A function that only calls console.log shows its result in the Console and hands back undefined. A function that uses return hands the value back, so the rest of your code can use it:

function addAndLog(a, b) { console.log(a + b); }
function addAndReturn(a, b) { return a + b; }

const r1 = addAndLog(3, 5);     // Console shows 8; r1 is undefined
const r2 = addAndReturn(3, 5);  // r2 is 8
r1 * r1;                        // NaN
r2 * r2;                        // 64
Which branch runs#

An if / else if chain checks its conditions from the top and stops at the first one that is true. Reorder the branches and the same score can get a different grade:

const score = 95;
if (score >= 80) { console.log("B"); }
else if (score >= 90) { console.log("A"); }   // never reached for 95

The order of the branches is a business rule. Use === to compare, which also checks the type: 5 == "5" is true, and 5 === "5" is false.

Writing getDiscount yourself#

The rules are on the Member Discount slide: 20 for a total of 100 or more and a member, 10 for one of the two, 0 otherwise.

  1. Ask your agent for discount.html, with a cart total, a member checkbox and a button, and with getDiscount(total, isMember) left empty
  2. Write the function yourself
  3. Test 150 with and without the checkbox, and 50 with and without it
  4. Ask your agent to check your function

A correction: you can put one if inside another. Both of these work:

// && and ||
if (total >= 100 && isMember) { return 20; }
else if (total >= 100 || isMember) { return 10; }
else { return 0; }

// one if inside another
if (total >= 100) {
  if (isMember) { return 20; } else { return 10; }
} else {
  if (isMember) { return 10; } else { return 0; }
}
The walkthrough question#

In the to-do app, if (text) is what stops an empty task from being added. Delete that line, type only spaces, click Add, and an empty task appears. That is the kind of question to expect at a showcase: you do not need to have written the line, but you need to be able to state why it is there and what the page will do without it.