Skip to content

Commit 8397ef9

Browse files
implemented recommendations and made appropriate changes
1 parent 388cbae commit 8397ef9

1 file changed

Lines changed: 34 additions & 23 deletions

File tree

debugging/book-library/script.js

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
let myLibrary = JSON.parse(localStorage.getItem("myLibrary")) || [];
1+
const myLibrary = [];
22

33
function isValidAuthor(text) {
44
for (let char of text) {
@@ -29,7 +29,6 @@ function isValidTitle(text) {
2929
}
3030
return true;
3131
}
32-
// 2. then your other functions (populateStorage, render, etc)
3332

3433
function saveLibrary() {
3534
localStorage.setItem("myLibrary", JSON.stringify(myLibrary));
@@ -53,22 +52,29 @@ document.getElementById("bookForm").addEventListener("submit", function (e) {
5352
return;
5453
}
5554

56-
populateStorage(title, author, pages, read);
55+
const success = populateStorage(title, author, pages, read);
56+
57+
if (!success) return; // ❗ STOP if invalid
5758

5859
render();
60+
saveLibrary();
5961

6062
this.reset();
61-
6263
$("#demo").collapse("hide");
6364
});
6465

6566
//check the right input from forms and if its ok -> add the new book (object in array)
6667
//via Book function and start render function
6768

6869
function populateStorage(title, author, pages, check) {
70+
if (!/^\d+$/.test(pages) || Number(pages) < 1 || Number(pages) > 5000) {
71+
alert("Pages must be a whole number between 1 and 5000");
72+
return false;
73+
}
74+
6975
let newBook = new Book(title, author, pages, check);
7076
myLibrary.push(newBook);
71-
saveLibrary();
77+
return true;
7278
}
7379

7480
function Book(title, author, pages, check) {
@@ -79,21 +85,21 @@ function Book(title, author, pages, check) {
7985
}
8086

8187
function render() {
82-
const table = document.querySelector("#display tbody");
83-
table.innerHTML = "";
88+
const tableBody = document.querySelector("#display tbody");
89+
tableBody.innerHTML = "";
8490

8591
//insert updated row and cells
8692
let length = myLibrary.length;
8793
for (let i = 0; i < length; i++) {
88-
let row = table.insertRow();
94+
let row = tableBody.insertRow();
8995
let titleCell = row.insertCell(0);
9096
let authorCell = row.insertCell(1);
9197
let pagesCell = row.insertCell(2);
9298
let wasReadCell = row.insertCell(3);
9399
let deleteCell = row.insertCell(4);
94-
titleCell.innerHTML = myLibrary[i].title;
95-
authorCell.innerHTML = myLibrary[i].author;
96-
pagesCell.innerHTML = myLibrary[i].pages;
100+
titleCell.innerText = myLibrary[i].title;
101+
authorCell.innerText = myLibrary[i].author;
102+
pagesCell.innerText = myLibrary[i].pages;
97103

98104
//add and wait for action for read/unread button
99105
let changeBut = document.createElement("button");
@@ -121,6 +127,7 @@ function render() {
121127
deleteCell.appendChild(delBut);
122128

123129
delBut.addEventListener("click", function () {
130+
alert(`You've deleted title: ${myLibrary[i].title}`);
124131
myLibrary.splice(i, 1);
125132

126133
saveLibrary();
@@ -131,17 +138,21 @@ function render() {
131138
}
132139

133140
function loadDefaultBooks() {
134-
if (myLibrary.length === 0) {
135-
populateStorage("Robinson Crusoe", "Daniel Defoe", "252", true);
136-
populateStorage(
137-
"The Old Man and the Sea",
138-
"Ernest Hemingway",
139-
"127",
140-
false
141-
);
142-
}
141+
myLibrary.push(
142+
new Book("Robinson Crusoe", "Daniel Defoe", "252", true),
143+
new Book("The Old Man and the Sea", "Ernest Hemingway", "127", false)
144+
);
143145
}
144-
window.addEventListener("load", function () {
145-
loadDefaultBooks();
146+
147+
window.onload = () => {
148+
myLibrary.length = 0;
149+
150+
const saved = localStorage.getItem("myLibrary");
151+
152+
if (saved && saved !== "[]") {
153+
myLibrary.push(...JSON.parse(saved));
154+
} else {
155+
loadDefaultBooks();
156+
}
146157
render();
147-
});
158+
};

0 commit comments

Comments
 (0)