From db7e56d0eeb0c9f5b32e74e4a6c7ff9c61d0f2d9 Mon Sep 17 00:00:00 2001 From: Ales-Os-Dev_Lab Date: Fri, 17 Apr 2026 09:30:56 +0100 Subject: [PATCH 1/4] Fix book submission logic and improve render function; add author to Book object --- debugging/book-library/script.js | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 75ce6c1d..01cec616 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -31,14 +31,16 @@ function submit() { if ( title.value == null || title.value == "" || + author.value == null || + author.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); + let book = new Book(title.value, author.value, pages.value, check.checked); + myLibrary.push(book); render(); } } @@ -54,7 +56,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 +78,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"; @@ -90,11 +92,11 @@ function 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 () { + delButton.id = i + 5; + deleteCell.appendChild(delButton); + delButton.className = "btn btn-warning"; + delButton.innerHTML = "Delete"; + delButton.addEventListener("click", function () { alert(`You've deleted title: ${myLibrary[i].title}`); myLibrary.splice(i, 1); render(); From b2e66c18a31e8e6eeb5b0711a854c13b5ce91355 Mon Sep 17 00:00:00 2001 From: Ales-Os-Dev_Lab Date: Thu, 23 Apr 2026 15:24:10 +0100 Subject: [PATCH 2/4] Refactor event listener and validation logic; update Book class to use ES6 syntax --- debugging/book-library/script.js | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 01cec616..30def26d 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,6 +1,6 @@ let myLibrary = []; -window.addEventListener("load", function (e) { +window.addEventListener("load", function() { populateStorage(); render(); }); @@ -29,12 +29,10 @@ const check = document.getElementById("check"); //via Book function and start render function function submit() { if ( - title.value == null || - title.value == "" || - author.value == null || - author.value == "" || - pages.value == null || - pages.value == "" + title.value === "" || + author.value === "" || + pages.value === "" || + !check.checked //if the checkbox is not checked, it will be false, so we need to check if its not checked ) { alert("Please fill all fields!"); return false; @@ -45,11 +43,13 @@ function submit() { } } -function Book(title, author, pages, check) { - this.title = title; - this.author = author; - this.pages = pages; - this.check = check; +class Book { + constructor(title, author, pages, check) { + this.title = title; + this.author = author; + this.pages = pages; + this.check = check; + } } function render() { @@ -78,7 +78,7 @@ function render() { changeBut.className = "btn btn-success"; wasReadCell.appendChild(changeBut); let readStatus = ""; - if (myLibrary[i].check == true) { + if (myLibrary[i].check) { readStatus = "Yes"; } else { readStatus = "No"; @@ -92,7 +92,7 @@ function render() { //add delete button to every row and render again let delButton = document.createElement("button"); - delButton.id = i + 5; + delButton.id = i; deleteCell.appendChild(delButton); delButton.className = "btn btn-warning"; delButton.innerHTML = "Delete"; From fbccd0aa03704ca688d43a9b93ed63363bcd132a Mon Sep 17 00:00:00 2001 From: Ales-Os-Dev_Lab Date: Thu, 23 Apr 2026 17:46:29 +0100 Subject: [PATCH 3/4] Improve form validation and submission logic; ensure fields are trimmed and reset correctly --- debugging/book-library/script.js | 46 ++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 30def26d..858a9b5a 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -28,20 +28,38 @@ 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 === "" || - author.value === "" || - pages.value === "" || - !check.checked //if the checkbox is not checked, it will be false, so we need to check if its not checked - ) { - alert("Please fill all fields!"); + + // 1. Preprocess and store cleaned values in variables / removes spaces from the beginning and end + const cleanTitle = title.value.trim(); + const cleanAuthor = author.value.trim(); + + // Convert string input to a Number for calculation/validation + const pageCount = Number(pages.value); + const isRead = check.checked; + + // 2. VALIDATION: // Check if strings are empty after trimming spaces + if (cleanTitle === "" || cleanAuthor === "") { + alert("Title and Author cannot be empty!"); + return false; + } + // Check if page count is a valid number and greater than 0 + if (isNaN(pageCount) || pageCount <= 0) { + alert("Please enter a valid number of pages!"); return false; - } else { - let book = new Book(title.value, author.value, pages.value, check.checked); - myLibrary.push(book); + } + + // 3. If all validations pass, create a new Book object and add it to the library + let book = new Book(cleanTitle, cleanAuthor, pageCount, isRead); + myLibrary.push(book); + + // 4. Clear the form fields after submission + title.value = ""; + author.value = ""; + pages.value = ""; + check.checked = false; + render(); } -} class Book { constructor(title, author, pages, check) { @@ -68,9 +86,9 @@ function render() { 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; + 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"); From 276d98b1c91e8a943e4a489a844a38f8e5e5f877 Mon Sep 17 00:00:00 2001 From: Ales-Os-Dev_Lab Date: Thu, 23 Apr 2026 22:21:41 +0100 Subject: [PATCH 4/4] Refactor code for improved readability and consistency; streamline form submission and rendering logic Co-authored-by: Copilot --- debugging/book-library/script.js | 62 ++++++++++++++------------------ 1 file changed, 27 insertions(+), 35 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 858a9b5a..35920d8b 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,6 +1,6 @@ let myLibrary = []; -window.addEventListener("load", function() { +window.addEventListener("load", function () { populateStorage(); render(); }); @@ -28,13 +28,12 @@ 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() { - - // 1. Preprocess and store cleaned values in variables / removes spaces from the beginning and end + // 1. Preprocess and store cleaned values in variables / removes spaces from the beginning and end const cleanTitle = title.value.trim(); const cleanAuthor = author.value.trim(); - + // Convert string input to a Number for calculation/validation - const pageCount = Number(pages.value); + const pageCount = Number(pages.value); const isRead = check.checked; // 2. VALIDATION: // Check if strings are empty after trimming spaces @@ -53,13 +52,13 @@ function submit() { myLibrary.push(book); // 4. Clear the form fields after submission - title.value = ""; - author.value = ""; - pages.value = ""; - check.checked = false; - - render(); - } + title.value = ""; + author.value = ""; + pages.value = ""; + check.checked = false; + + render(); +} class Book { constructor(title, author, pages, check) { @@ -71,37 +70,30 @@ class Book { } function render() { - let table = document.getElementById("display"); - let rowsNumber = table.rows.length; + const table = document.getElementById("display"); + const tbody = table.querySelector("tbody"); //delete old table - for (let n = rowsNumber - 1; n > 0; n--) { - table.deleteRow(n); + if (tbody) { + tbody.innerHTML = ""; } //insert updated row and cells - let length = myLibrary.length; + const 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); + const row = table.insertRow(-1); + 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; + const changeBut = document.createElement("button"); changeBut.className = "btn btn-success"; wasReadCell.appendChild(changeBut); - let readStatus = ""; - if (myLibrary[i].check) { - readStatus = "Yes"; - } else { - readStatus = "No"; - } - changeBut.innerText = readStatus; + changeBut.innerText = myLibrary[i].check ? "Yes" : "No"; changeBut.addEventListener("click", function () { myLibrary[i].check = !myLibrary[i].check; @@ -109,15 +101,15 @@ function render() { }); //add delete button to every row and render again - let delButton = document.createElement("button"); - delButton.id = i; + const delButton = document.createElement("button"); deleteCell.appendChild(delButton); delButton.className = "btn btn-warning"; delButton.innerHTML = "Delete"; delButton.addEventListener("click", function () { - alert(`You've deleted title: ${myLibrary[i].title}`); + const deletedTitle = myLibrary[i].title; myLibrary.splice(i, 1); render(); + alert(`You've deleted title: ${deletedTitle}`); }); } }