From d143acf7b7478088fdb70577002d9e2b9a52509c Mon Sep 17 00:00:00 2001 From: cyf Date: Sat, 15 Aug 2026 13:23:58 +0100 Subject: [PATCH 1/5] Fix book library form markup --- debugging/book-library/index.html | 82 +++++++++++++++---------------- 1 file changed, 39 insertions(+), 43 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 23acfa71e..94da673d8 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,7 +1,7 @@ - + - + Book Library Library Add new book -
-
- - - - - - -
+ @@ -93,4 +89,4 @@

Library

- + \ No newline at end of file From da01b0ca85876830a62131486dd2029ce6b587ab Mon Sep 17 00:00:00 2001 From: cyf Date: Sat, 15 Aug 2026 13:25:07 +0100 Subject: [PATCH 2/5] Fix book library functionality --- debugging/book-library/script.js | 135 ++++++++++++++++--------------- 1 file changed, 71 insertions(+), 64 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 75ce6c1d3..0d837cbe2 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,4 +1,12 @@ -let myLibrary = []; +const myLibrary = []; + +const titleInput = document.getElementById("title"); +const authorInput = document.getElementById("author"); +const pagesInput = document.getElementById("pages"); +const checkInput = document.getElementById("check"); +const bookForm = document.getElementById("bookForm"); + +bookForm.addEventListener("submit", addBook); window.addEventListener("load", function (e) { populateStorage(); @@ -6,41 +14,40 @@ window.addEventListener("load", function (e) { }); function populateStorage() { - if (myLibrary.length == 0) { - let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true); - let book2 = new Book( + if (myLibrary.length === 0) { + const book1 = new Book("Robinson 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(); } } +function addBook(event) { + event.preventDefault(); -const title = document.getElementById("title"); -const author = document.getElementById("author"); -const pages = document.getElementById("pages"); -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() { - 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, title.value, pages.value, check.checked); - library.push(book); - render(); + const title = titleInput.value.trim(); + const author = authorInput.value.trim(); + const pages = Number(pagesInput.value); + const check = checkInput.checked; + //check the right input from forms and if its ok -> add the new book (object in array) + //via Book function and start render function + if (!title || !author || !pages || pages < 1 || !Number.isInteger(pages)) { + alert("Please fill all fields correctly!"); + return; } + + const book = new Book(title, author, pages, checkInput.checked); + + myLibrary.push(book); + render(); + + bookForm.reset(); } function Book(title, author, pages, check) { @@ -51,53 +58,53 @@ function Book(title, author, pages, check) { } function render() { - let table = document.getElementById("display"); - let rowsNumber = table.rows.length; + const tbody = document.querySelector("#display tbody"); + tbody.innerHTML = ""; //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; + for (let i = 0; i < myLibrary.length; i++) { + //insert updated row and cells + const row = tbody.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; //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 == false) { - readStatus = "Yes"; - } else { - readStatus = "No"; - } - changeBut.innerText = readStatus; - - changeBut.addEventListener("click", function () { + // Read button + const readButton = document.createElement("button"); + readButton.className = "btn btn-success"; + + // simplified if/else using ternary + readButton.textContent = myLibrary[i].check ? "Yes" : "No"; + + wasReadCell.appendChild(readButton); + + readButton.addEventListener("click", function () { myLibrary[i].check = !myLibrary[i].check; render(); }); //add delete button to every row and render again - let delButton = document.createElement("button"); - delBut.id = i + 5; - deleteCell.appendChild(delBut); - delBut.className = "btn btn-warning"; - delBut.innerHTML = "Delete"; - delBut.addEventListener("clicks", function () { - alert(`You've deleted title: ${myLibrary[i].title}`); + const deleteButton = document.createElement("button"); + deleteButton.className = "btn btn-warning"; + deleteButton.textContent = "Delete"; + + deleteCell.appendChild(deleteButton); + + deleteButton.addEventListener("click", function () { + const deletedTitle = myLibrary[i].title; + + // delete first, then render myLibrary.splice(i, 1); render(); + + alert(`You've deleted title: ${deletedTitle}`); }); } -} +} \ No newline at end of file From 87f470214d5f2cb54bab8b4374a995482a2cd44f Mon Sep 17 00:00:00 2001 From: cyf Date: Sat, 15 Aug 2026 15:04:50 +0100 Subject: [PATCH 3/5] Fix HTML validation errors --- debugging/book-library/index.html | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 94da673d8..b9da855f5 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -2,19 +2,19 @@ Book Library - + + + + + - + From 4328a6c3fc4a248a24b301748ad6ad6c297e854f Mon Sep 17 00:00:00 2001 From: cyf Date: Sat, 15 Aug 2026 15:05:38 +0100 Subject: [PATCH 4/5] Organize book library initialization --- debugging/book-library/script.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 0d837cbe2..a01641d09 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -6,12 +6,13 @@ const pagesInput = document.getElementById("pages"); const checkInput = document.getElementById("check"); const bookForm = document.getElementById("bookForm"); -bookForm.addEventListener("submit", addBook); - -window.addEventListener("load", function (e) { +function init() { + bookForm.addEventListener("submit", addBook); populateStorage(); render(); -}); +} + +window.addEventListener("load", init); function populateStorage() { if (myLibrary.length === 0) { From 337a9f84a3d5ae275263d338ea5ff67a5ecfc8e7 Mon Sep 17 00:00:00 2001 From: cyf Date: Sat, 15 Aug 2026 15:07:07 +0100 Subject: [PATCH 5/5] Allow UI update before delete alert --- debugging/book-library/script.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index a01641d09..efb9c1af0 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -101,11 +101,12 @@ function render() { deleteButton.addEventListener("click", function () { const deletedTitle = myLibrary[i].title; - // delete first, then render myLibrary.splice(i, 1); render(); - alert(`You've deleted title: ${deletedTitle}`); + setTimeout(() => { + alert(`You've deleted title: ${deletedTitle}`); + }, 0); }); } } \ No newline at end of file