CSE 331: Software Design and Implementation, Autumn 2026
Your job is to deliver code that you know works.
load and post hand the server's messages to showshow puts one <li> in the tree for each, and keeps nothing10function show(messages) {
11 const list = document.getElementById("messages");
12 list.innerHTML = "";
13 for (const message of messages) {
14 const item = document.createElement("li");
15 item.textContent = message;
16 list.appendChild(item);
17 }
18}8Show only: <input id="only">43function filter() {
44 const only = document.getElementById("only").value;
45 const list = document.getElementById("messages");
46 for (const item of list.querySelectorAll("li")) {
47 if (!item.textContent.startsWith(only)) {
48 list.removeChild(item);
49 }
50 }
51}
52
53document.getElementById("post").onclick = post;
54document.getElementById("only").oninput = filter;ada. What happens to bob's message?bob: what does the list show?show drew the tree from the messages, and kept nothing18function show() {
19 const only = document.getElementById("only").value;
20 const list = document.getElementById("messages");
21 list.innerHTML = "";
22 for (const message of messages) {
23 if (message.startsWith(only)) {
24 const item = document.createElement("li");
25 item.textContent = message;
26 list.appendChild(item);
27 }
28 }
29}show, on line 13 of app.js: let messages = [];showada, clear the box, type bob: what does the list show?messages.push("eve: yo"). The page?> messages.push("eve: yo") 3 > show() undefined
The list on the page shows exactly the messages that start with the text in the filter box, which is all of them when the box is empty.
let messagesshow() made it true againshow runs33function update(sent) {
34 messages = sent;
35 show();
36}load and post hand what the server sent to updateupdate assigns messages, and it redrawsmessages and forget show, and the claim is false| Lecture 11's class | The guestbook |
|---|---|
| its fields | messages and the tree |
| its methods | the callbacks: load, post, show |
| the constructor | load's first run |
| true when no method runs | true between events |
Lecture 12: await cuts a function in two, so make the claim true before it
HttpServer calls handleshow, so nobody can forget to9function Guestbook() {
10 const [messages, setMessages] = useState([]);
11 const [only, setOnly] = useState("");
12
13 async function load() {
14 const response = await fetch("/api/messages");
15 setMessages(await response.json());
16 }
17 useEffect(() => { load(); }, []);useState returns the state and its setter; => writes a functionsetMessages is update with the redraw built in19const shown = messages.filter(
20 (message) => message.startsWith(only));
21return (
22 <div>
23 Show only: <input value={only}
24 onChange={(event) => setOnly(event.target.value)} />
25 <ul>
26 {shown.map((message, i) => <li key={i}>{message}</li>)}
27 </ul>
28 </div>
29);