Exam 02 Practice (Review Day)
1. Event handler workflow
Imagine the user clicks a bookmark button on a post.
Write out the sequence of steps that should happen from the click event to the updated UI. Your answer should mention:
- the event handler
- the API request
- the HTTP method
- the DOM or state update
2. Callback meaning
For each of the following, explain what the function inside the parentheses does:
posts.map((post) => `<p>${post.caption}</p>`)
posts.filter((post) => post.bookmarked)
posts.toSorted((a, b) => b.likes - a.likes)
3. Fetch + authentication
Write a short fetch example that sends a POST request to /api/bookmarks with:
- a JSON body
- a
Content-Typeheader - a bearer token in the
Authorizationheader
4. Debug the async bug
What is wrong with this code (there are at least 2 errors), and how would you fix it?
function loadProfile() {
const response = await fetch('/api/profile');
const data = response.json();
console.log(data);
}
5. Pick the best tool
For each task below, choose the best tool:
forEachmapfiltertoSorted
Tasks:
- Keep only posts that belong to the current user.
- Turn an array of posts into an array of HTML strings.
- Loop through an array and append each result to the page.
- Create a sorted copy of posts from highest likes to lowest likes.