Schedule > Review: Higher-Order Functions + Fetch Workflow
Due Mon, 03/23 at 11:59pm
1. JavaScript Review
Let’s walk through these questions. Try testing your ideas in the console or in a small .js file if needed.
1.1. Choose the right array method
For each task below, decide whether forEach, map, filter, or toSorted is the best tool:
- Create a new array containing only the posts that belong to user
7. - Create a new array of just the usernames from an array of user objects.
- Loop through an array of comments and insert each one into the DOM.
- Create a new array of posts ordered from most likes to fewest likes without changing the original array.
1.2. Predict the output: map
What will this code return?
const numbers = [2, 4, 6];
const stuff = numbers.map((num) => num * 2);
console.log(stuff);
Then answer:
- Does
mapchange the originalnumbersarray? - Why is
mapoften useful when generating HTML strings from data?
1.3. Predict the output: filter
What will this code return?
const posts = [
{ id: 1, bookmarked: true },
{ id: 2, bookmarked: false },
{ id: 3, bookmarked: true }
];
const savedPosts = posts.filter((post) => post.bookmarked);
console.log(savedPosts);
Then answer:
- How many objects will be in
savedPosts? - What is the difference between
filterandmap?
1.4. Fix the sorting bug
Why can this code produce the wrong result?
const likes = [3, 18, 2, 25];
console.log(likes.sort((a, b) => a - b));
Rewrite it so that:
- the values are sorted from largest to smallest
- the original
likesarray is not mutated
1.5. Fetch workflow review
Put these steps in the correct order:
async function displayserverDataToDOM() {
// 1. Update the DOM with the returned data.
// 2. Build the URL and request options.
// 3. Read a search term or id from the DOM.
// 4. Convert the response to JSON.
// 5. Call `fetch(...)`.
}
Then discuss:
- Where does
awaitbelong in this process? - What should your code do if
response.okisfalse?
1.6. HTTP method matching
Match each action to the most appropriate HTTP method:
- Load the current user’s profile
- Create a new bookmark
- Remove an existing bookmark
- Load the list of posts
Choose from: GET, POST, DELETE
Then explain:
- Which of these requests usually needs a request body?
- Which of these requests should change data on the server?
1.7. Take the quizzes (on your own)
Navigate to the quizzes page and take the following quizzes for practice:
- JavaScript Array Methods: map, filter, reduce, forEach
- HTTP Methods + Fetch API with Async/Await
2. Finishing Tutorial 8
2.1. Conditional rendering of a bookmark
- If the logged in user has bookmarked the post, the post will have a
current_user_bookmark_idfield and the bookmark should be drawn as filled in using this Font Awesome icon:<i class="fas fa-bookmark"></i> - If the logged in user has NOT bookmarked the post, the post’s
current_user_bookmark_idfield will be will undefined, and the bookmark should be drawn as hollow using this Font Awesome icon:<i class="far fa-bookmark"></i> - Make sure you use the
aria-labelattribute so that screen readers can interpret the icon (because icons aren’t perceptable to people who are blind).
2.2. Complete the create / delete bookmark functionality
- If the user clicks a hollow bookmark, bookmark the post (POST request to
/api/bookmarks). - If the user clicks a filled in bookmark resource, delete the bookmark (DELETE request to
/api/bookmarks/:id).