Skip to content

Commit d24a199

Browse files
Fix library rendering, localStorage loading, and delete logic
1 parent 8397ef9 commit d24a199

1 file changed

Lines changed: 19 additions & 18 deletions

File tree

debugging/book-library/script.js

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ function populateStorage(title, author, pages, check) {
8080
function Book(title, author, pages, check) {
8181
this.title = title;
8282
this.author = author;
83-
this.pages = pages;
83+
this.pages = Number(pages);
8484
this.check = check;
8585
}
8686

@@ -89,31 +89,28 @@ function render() {
8989
tableBody.innerHTML = "";
9090

9191
//insert updated row and cells
92-
let length = myLibrary.length;
93-
for (let i = 0; i < length; i++) {
92+
myLibrary.forEach((book, i) => {
9493
let row = tableBody.insertRow();
9594
let titleCell = row.insertCell(0);
9695
let authorCell = row.insertCell(1);
9796
let pagesCell = row.insertCell(2);
9897
let wasReadCell = row.insertCell(3);
9998
let deleteCell = row.insertCell(4);
100-
titleCell.innerText = myLibrary[i].title;
101-
authorCell.innerText = myLibrary[i].author;
102-
pagesCell.innerText = myLibrary[i].pages;
99+
titleCell.innerText = book.title;
100+
authorCell.innerText = book.author;
101+
pagesCell.innerText = book.pages;
103102

104103
//add and wait for action for read/unread button
105104
let changeBut = document.createElement("button");
106105

107-
changeBut.className = myLibrary[i].check
108-
? "btn btn-success"
109-
: "btn btn-secondary";
106+
changeBut.className = book.check ? "btn btn-success" : "btn btn-secondary";
110107

111-
changeBut.innerText = myLibrary[i].check ? "Read" : "Unread";
108+
changeBut.innerText = book.check ? "Read" : "Unread";
112109

113110
wasReadCell.appendChild(changeBut);
114111

115112
changeBut.addEventListener("click", function () {
116-
myLibrary[i].check = !myLibrary[i].check;
113+
book.check = !book.check;
117114
saveLibrary();
118115
render();
119116
});
@@ -127,14 +124,13 @@ function render() {
127124
deleteCell.appendChild(delBut);
128125

129126
delBut.addEventListener("click", function () {
130-
alert(`You've deleted title: ${myLibrary[i].title}`);
131-
myLibrary.splice(i, 1);
132-
127+
const deletedTitle = book.title;
128+
myLibrary.splice(myLibrary.indexOf(book), 1);
133129
saveLibrary();
134-
135130
render();
131+
alert(`You've deleted title: ${deletedTitle}`);
136132
});
137-
}
133+
});
138134
}
139135

140136
function loadDefaultBooks() {
@@ -149,8 +145,13 @@ window.onload = () => {
149145

150146
const saved = localStorage.getItem("myLibrary");
151147

152-
if (saved && saved !== "[]") {
153-
myLibrary.push(...JSON.parse(saved));
148+
if (saved) {
149+
const parsed = JSON.parse(saved);
150+
if (Array.isArray(parsed) && parsed.length > 0) {
151+
myLibrary.push(...parsed);
152+
} else {
153+
loadDefaultBooks();
154+
}
154155
} else {
155156
loadDefaultBooks();
156157
}

0 commit comments

Comments
 (0)