From bee0c61e2d80b90930b405da74c9e2d7e948511b Mon Sep 17 00:00:00 2001 From: abduhasen Date: Sat, 8 Aug 2026 16:42:40 +0100 Subject: [PATCH 1/4] debugging code using devtools --- debugging/book-library/index.html | 17 +++++++++-------- debugging/book-library/script.js | 12 ++++++------ 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 23acfa71..f1fde438 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,4 +1,4 @@ - + @@ -65,7 +65,8 @@

Library

type="submit" value="Submit" class="btn btn-primary" - onclick="submit();" + id="submit" + onclick="submitBook()" /> @@ -77,16 +78,16 @@

Library

Author Number of Pages Read - + Delete - - - - - + Title + Author + Number of page + Read + Delete diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 75ce6c1d..4156df0e 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -27,7 +27,7 @@ const check = document.getElementById("check"); //check the right input from forms and if its ok -> add the new book (object in array) //via Book function and start render function -function submit() { +function submitBook() { if ( title.value == null || title.value == "" || @@ -38,7 +38,7 @@ function submit() { return false; } else { let book = new Book(title.value, title.value, pages.value, check.checked); - library.push(book); + myLibrary.push(book); render(); } } @@ -54,7 +54,7 @@ function render() { let table = document.getElementById("display"); let rowsNumber = table.rows.length; //delete old table - for (let n = rowsNumber - 1; n > 0; n-- { + for (let n = rowsNumber - 1; n > 0; n--) { table.deleteRow(n); } //insert updated row and cells @@ -76,7 +76,7 @@ function render() { changeBut.className = "btn btn-success"; wasReadCell.appendChild(changeBut); let readStatus = ""; - if (myLibrary[i].check == false) { + if (myLibrary[i].check == true) { readStatus = "Yes"; } else { readStatus = "No"; @@ -89,12 +89,12 @@ function render() { }); //add delete button to every row and render again - let delButton = document.createElement("button"); + let delBut = document.createElement("button"); delBut.id = i + 5; deleteCell.appendChild(delBut); delBut.className = "btn btn-warning"; delBut.innerHTML = "Delete"; - delBut.addEventListener("clicks", function () { + delBut.addEventListener("click", function () { alert(`You've deleted title: ${myLibrary[i].title}`); myLibrary.splice(i, 1); render(); From a32f0ae52e524ceeafde35276d99c07b5aa71736 Mon Sep 17 00:00:00 2001 From: abduhasen Date: Sun, 9 Aug 2026 14:54:09 +0100 Subject: [PATCH 2/4] fixing input value --- debugging/book-library/script.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 4156df0e..9a6692db 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -37,7 +37,7 @@ function submitBook() { alert("Please fill all fields!"); return false; } else { - let book = new Book(title.value, title.value, pages.value, check.checked); + let book = new Book(title.value, author.value, pages.value, check.checked); myLibrary.push(book); render(); } From 15163c15d05c77f4451f8db4c02baa5f48e6406f Mon Sep 17 00:00:00 2001 From: abduhasen Date: Mon, 10 Aug 2026 15:02:31 +0100 Subject: [PATCH 3/4] Improved codes and fixed them based on a given `General Feedback for Possible Improvement` --- debugging/book-library/index.html | 64 +++++++-------- debugging/book-library/script.js | 132 ++++++++++++++++-------------- 2 files changed, 101 insertions(+), 95 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index f1fde438..325b3800 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,20 +1,20 @@ - + - - + + + + Library + + - + @@ -23,52 +23,57 @@

Library

Add books to your virtual library

-
-
+
- + + + + - -
+ + +
@@ -81,17 +86,10 @@

Library

- - - - - - - - - + +
Delete
TitleAuthorNumber of pageReadDelete
- + diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 9a6692db..a3e15b1a 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,46 +1,58 @@ let myLibrary = []; -window.addEventListener("load", function (e) { +window.addEventListener("load", function () { populateStorage(); render(); }); function populateStorage() { if (myLibrary.length == 0) { - let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true); - let book2 = new Book( + const book1 = new Book("Robison Crusoe", "Daniel Defoe", 252, true); + const book2 = new Book( "The Old Man and the Sea", "Ernest Hemingway", - "127", + 127, true ); myLibrary.push(book1); myLibrary.push(book2); - render(); } } -const title = document.getElementById("title"); -const author = document.getElementById("author"); -const pages = document.getElementById("pages"); -const check = document.getElementById("check"); +const titleInput = document.getElementById("title"); +const authorInput = document.getElementById("author"); +const pagesInput = document.getElementById("pages"); +const readCheckbox = document.getElementById("check"); +const bookForm = document.getElementById("bookForm"); +const bookList = document.getElementById("bookList"); + +bookForm.addEventListener("submit", function (e) { + e.preventDefault(); + submitBook(); +}); //check the right input from forms and if its ok -> add the new book (object in array) //via Book function and start render function function submitBook() { - if ( - title.value == null || - title.value == "" || - pages.value == null || - pages.value == "" - ) { - alert("Please fill all fields!"); - return false; - } else { - let book = new Book(title.value, author.value, pages.value, check.checked); - myLibrary.push(book); - render(); + const title = titleInput.value.trim(); + const author = authorInput.value.trim(); + const pageCount = Number(pagesInput.value); + + if (title === "" || author === "") { + return; } + + if (!Number.isInteger(pageCount) || pageCount < 1) { + return; + } + + const book = new Book(title, author, pageCount, readCheckbox.checked); + + myLibrary.push(book); + + render(); + + bookForm.reset(); } function Book(title, author, pages, check) { @@ -51,53 +63,49 @@ function Book(title, author, pages, check) { } function render() { - let table = document.getElementById("display"); - let rowsNumber = table.rows.length; - //delete old table - for (let n = rowsNumber - 1; n > 0; n--) { - table.deleteRow(n); - } - //insert updated row and cells - let length = myLibrary.length; - for (let i = 0; i < length; i++) { - let row = table.insertRow(1); - let titleCell = row.insertCell(0); - let authorCell = row.insertCell(1); - let pagesCell = row.insertCell(2); - let wasReadCell = row.insertCell(3); - let deleteCell = row.insertCell(4); - titleCell.innerHTML = myLibrary[i].title; - authorCell.innerHTML = myLibrary[i].author; - pagesCell.innerHTML = myLibrary[i].pages; - - //add and wait for action for read/unread button - let changeBut = document.createElement("button"); - changeBut.id = i; - changeBut.className = "btn btn-success"; - wasReadCell.appendChild(changeBut); - let readStatus = ""; - if (myLibrary[i].check == true) { - readStatus = "Yes"; - } else { - readStatus = "No"; - } - changeBut.innerText = readStatus; - - changeBut.addEventListener("click", function () { + bookList.innerHTML = ""; + + for (let i = 0; i < myLibrary.length; i++) { + const row = bookList.insertRow(); + + const titleCell = row.insertCell(0); + const authorCell = row.insertCell(1); + const pagesCell = row.insertCell(2); + const wasReadCell = row.insertCell(3); + const deleteCell = row.insertCell(4); + + titleCell.textContent = myLibrary[i].title; + authorCell.textContent = myLibrary[i].author; + pagesCell.textContent = myLibrary[i].pages; + + // Read button + const changeBtn = document.createElement("button"); + changeBtn.className = "btn btn-success"; + + const readStatus = myLibrary[i].check ? "Yes" : "No"; + changeBtn.textContent = readStatus; + + wasReadCell.appendChild(changeBtn); + + changeBtn.addEventListener("click", function () { myLibrary[i].check = !myLibrary[i].check; render(); }); - //add delete button to every row and render again - let delBut = document.createElement("button"); - delBut.id = i + 5; - deleteCell.appendChild(delBut); - delBut.className = "btn btn-warning"; - delBut.innerHTML = "Delete"; - delBut.addEventListener("click", function () { - alert(`You've deleted title: ${myLibrary[i].title}`); + // Delete button + const deleteBtn = document.createElement("button"); + deleteBtn.className = "btn btn-warning"; + deleteBtn.textContent = "Delete"; + + deleteCell.appendChild(deleteBtn); + + deleteBtn.addEventListener("click", function () { + const deletedTitle = myLibrary[i].title; + myLibrary.splice(i, 1); render(); + + alert(`You've deleted title: ${deletedTitle}`); }); } } From fecbfd549d828fc50c7fd3dbd6206b058b1b21b2 Mon Sep 17 00:00:00 2001 From: abduhasen Date: Tue, 11 Aug 2026 12:04:57 +0100 Subject: [PATCH 4/4] fixing code based on mentor feedback --- debugging/book-library/script.js | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index a3e15b1a..d2cac560 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,9 +1,15 @@ -let myLibrary = []; +const myLibrary = []; window.addEventListener("load", function () { populateStorage(); render(); }); +const titleInput = document.getElementById("title"); +const authorInput = document.getElementById("author"); +const pagesInput = document.getElementById("pages"); +const readCheckbox = document.getElementById("check"); +const bookForm = document.getElementById("bookForm"); +const bookList = document.getElementById("bookList"); function populateStorage() { if (myLibrary.length == 0) { @@ -19,13 +25,6 @@ function populateStorage() { } } -const titleInput = document.getElementById("title"); -const authorInput = document.getElementById("author"); -const pagesInput = document.getElementById("pages"); -const readCheckbox = document.getElementById("check"); -const bookForm = document.getElementById("bookForm"); -const bookList = document.getElementById("bookList"); - bookForm.addEventListener("submit", function (e) { e.preventDefault(); submitBook();