Skip to content

Commit 5f7b018

Browse files
committed
Fix: syntax errors
1 parent 3a422e9 commit 5f7b018

1 file changed

Lines changed: 52 additions & 34 deletions

File tree

‎debugging/book-library/script.js‎

Lines changed: 52 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,44 @@ const author = document.getElementById("author");
2525
const pages = document.getElementById("pages");
2626
const check = document.getElementById("check");
2727

28-
//check the right input from forms and if its ok -> add the new book (object in array)
29-
//via Book function and start render function
3028
function submit() {
29+
// Validate all fields (including author)
3130
if (
3231
title.value == null ||
33-
title.value == "" ||
32+
title.value.trim() == "" ||
33+
author.value == null ||
34+
author.value.trim() == "" ||
3435
pages.value == null ||
35-
pages.value == ""
36+
pages.value.trim() == ""
3637
) {
37-
alert("Please fill all fields!");
38+
alert("Please fill all fields (Title, Author, Pages)!");
39+
return false;
40+
}
41+
42+
// Validate pages is a positive number
43+
const pagesNum = parseInt(pages.value, 10);
44+
if (isNaN(pagesNum) || pagesNum <= 0) {
45+
alert("Please enter a valid number of pages!");
3846
return false;
39-
} else {
40-
let book = new Book(title.value, title.value, pages.value, check.checked);
41-
library.push(book);
42-
render();
4347
}
48+
49+
// FIX: use author.value instead of title.value for the second argument
50+
let book = new Book(
51+
title.value.trim(),
52+
author.value.trim(),
53+
pagesNum,
54+
check.checked
55+
);
56+
myLibrary.push(book); // FIX: was 'library.push'
57+
render();
58+
59+
// Clear form fields for better UX
60+
title.value = "";
61+
author.value = "";
62+
pages.value = "";
63+
check.checked = false;
64+
65+
return true;
4466
}
4567

4668
function Book(title, author, pages, check) {
@@ -52,52 +74,48 @@ function Book(title, author, pages, check) {
5274

5375
function render() {
5476
let table = document.getElementById("display");
55-
let rowsNumber = table.rows.length;
56-
//delete old table
57-
for (let n = rowsNumber - 1; n > 0; n-- {
58-
table.deleteRow(n);
77+
78+
// Delete all rows except the header (row 0)
79+
// FIX: missing ')' in original for-loop – replaced with while loop for clarity
80+
while (table.rows.length > 1) {
81+
table.deleteRow(1);
5982
}
60-
//insert updated row and cells
61-
let length = myLibrary.length;
62-
for (let i = 0; i < length; i++) {
63-
let row = table.insertRow(1);
83+
84+
// Re-populate table with current library
85+
for (let i = 0; i < myLibrary.length; i++) {
86+
let row = table.insertRow(); // appends at the end (preserves order)
87+
6488
let titleCell = row.insertCell(0);
6589
let authorCell = row.insertCell(1);
6690
let pagesCell = row.insertCell(2);
6791
let wasReadCell = row.insertCell(3);
6892
let deleteCell = row.insertCell(4);
93+
6994
titleCell.innerHTML = myLibrary[i].title;
7095
authorCell.innerHTML = myLibrary[i].author;
7196
pagesCell.innerHTML = myLibrary[i].pages;
7297

73-
//add and wait for action for read/unread button
98+
// Read status toggle button
7499
let changeBut = document.createElement("button");
75-
changeBut.id = i;
76100
changeBut.className = "btn btn-success";
77-
wasReadCell.appendChild(changeBut);
78-
let readStatus = "";
79-
if (myLibrary[i].check == false) {
80-
readStatus = "Yes";
81-
} else {
82-
readStatus = "No";
83-
}
84-
changeBut.innerText = readStatus;
85-
101+
// FIX: correct "Yes" when read, "No" when not read
102+
changeBut.innerText = myLibrary[i].check ? "Yes" : "No";
86103
changeBut.addEventListener("click", function () {
87104
myLibrary[i].check = !myLibrary[i].check;
88105
render();
89106
});
107+
wasReadCell.appendChild(changeBut);
90108

91-
//add delete button to every row and render again
109+
// Delete button
92110
let delButton = document.createElement("button");
93-
delBut.id = i + 5;
94-
deleteCell.appendChild(delBut);
95-
delBut.className = "btn btn-warning";
96-
delBut.innerHTML = "Delete";
97-
delBut.addEventListener("clicks", function () {
111+
delButton.className = "btn btn-warning";
112+
delButton.innerHTML = "Delete";
113+
// FIX: event type 'click' (was 'clicks') and correct variable name
114+
delButton.addEventListener("click", function () {
98115
alert(`You've deleted title: ${myLibrary[i].title}`);
99116
myLibrary.splice(i, 1);
100117
render();
101118
});
119+
deleteCell.appendChild(delButton);
102120
}
103121
}

0 commit comments

Comments
 (0)