Skip to content

Commit b6e7bd0

Browse files
committed
I added a function that takes an array of book objects and displays them on the webpage
1 parent ce3a25b commit b6e7bd0

File tree

2 files changed

+74
-2
lines changed

2 files changed

+74
-2
lines changed

Sprint-3/reading-list/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<meta charset="utf-8" />
55
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
66
<link rel="stylesheet" href="style.css" />
7-
<title>Title here</title>
7+
<title>Reading list app</title>
88
</head>
99
<body>
1010
<div id="content">

Sprint-3/reading-list/script.js

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,72 @@
1-
// for the tests, do not modify this array of books
1+
// FUNCTION: readingList - This function takes an array of book objects and displays them on the webpage
2+
function readingList(books) {
3+
// STEP 1: Get the reading list container from the HTML
4+
// document.getElementById finds the <ul> element with id="reading-list" from our HTML
5+
// We store it in a variable called 'readingList' so we can add books to it later
6+
const readingList = document.getElementById("reading-list");
7+
8+
// STEP 2: Loop through each book in the array
9+
// books.forEach goes through each book object in the books array, one by one
10+
// For each book, it runs the code inside the curly braces {}
11+
books.forEach((book) => {
12+
// STEP 3: Create a list item for each book
13+
// document.createElement("li") makes a new empty <li> element (list item)
14+
// We store this new list item in a variable called 'listItem'
15+
// This <li> will be the container for one book's information
16+
const listItem = document.createElement("li");
17+
18+
// STEP 4: Set background color based on reading status
19+
// Check if the book has been read (book.alreadyRead is true or false)
20+
// If book.alreadyRead is true, make the background green
21+
// If book.alreadyRead is false, make the background red
22+
// This visually shows which books we've read and which we haven't
23+
if (book.alreadyRead) {
24+
listItem.style.backgroundColor = "green"; // Read books = green
25+
} else {
26+
listItem.style.backgroundColor = "red"; // Unread books = red
27+
}
28+
29+
// STEP 5: Create the book content as HTML string
30+
// We use backticks `` to create a template string that can span multiple lines
31+
// ${} inside the template lets us insert JavaScript variables
32+
// This creates the HTML structure for one book with:
33+
// - Book cover image
34+
// - Book title
35+
// - Book author
36+
// - Reading status text
37+
const bookContent = `
38+
<div class="book">
39+
<img src="${book.bookCoverImage}" alt="${book.title} cover">
40+
<div class="book-info">
41+
<h3>${book.title}</h3>
42+
<p>by ${book.author}</p>
43+
<p class="reading-status">${
44+
book.alreadyRead ? "Already read" : "Not read yet"
45+
}</p>
46+
</div>
47+
</div>
48+
`;
49+
50+
// STEP 6: Add the content to the list item
51+
// listItem.innerHTML takes our HTML string from bookContent
52+
// and puts it inside the <li> element we created earlier
53+
// Now our empty <li> is filled with the book's information
54+
listItem.innerHTML = bookContent;
55+
56+
// STEP 7: Add the list item to the reading list
57+
// readingList.appendChild takes our completed list item (with book content)
58+
// and adds it to the <ul> element on the webpage
59+
// This makes the book visible to the user
60+
readingList.appendChild(listItem);
61+
});
62+
}
63+
64+
// THE ARRAY OF BOOKS: This is our data - a list of book objects
65+
// Each book object has:
66+
// - title: the book's name
67+
// - author: who wrote the book
68+
// - alreadyRead: true/false if we've read it
69+
// - bookCoverImage: URL to the book's cover picture
270
const books = [
371
{
472
title: "The Design of Everyday Things",
@@ -21,3 +89,7 @@ const books = [
2189
},
2290
];
2391

92+
// STEP 8: Call the function to render the reading list
93+
// This executes the readingList function and passes our books array to it
94+
// The function will then create and display all the books on the webpage
95+
readingList(books);

0 commit comments

Comments
 (0)