From f1b0058c7a88eb28833ff7565fd6936c3d19fc6c Mon Sep 17 00:00:00 2001 From: galyna-k Date: Fri, 1 Aug 2025 13:56:31 +0200 Subject: [PATCH 01/12] fix the show of any books - add an omitted parenthesis --- 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 75ce6c1d..eed706fd 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -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 From b5721caf624c3aa784606dea64b7855bf6ac2fa9 Mon Sep 17 00:00:00 2001 From: galyna-k Date: Fri, 1 Aug 2025 14:08:00 +0200 Subject: [PATCH 02/12] fix error in console when trying to add a book - fix the variable name of library to the correct one; - fix the name of the delete button --- debugging/book-library/script.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index eed706fd..19c53ac2 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -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(); } } @@ -89,7 +89,7 @@ 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"; From d2faec121ba2821ba620c0a2b580d74e2a684729 Mon Sep 17 00:00:00 2001 From: galyna-k Date: Fri, 1 Aug 2025 14:10:40 +0200 Subject: [PATCH 03/12] correct the author's name when creating a new book --- 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 19c53ac2..f95d0887 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -37,7 +37,7 @@ function submit() { 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 385b33c142417aaa4d8a186af1461fb7bb6e97e4 Mon Sep 17 00:00:00 2001 From: galyna-k Date: Fri, 1 Aug 2025 14:26:17 +0200 Subject: [PATCH 04/12] fix typo in delete button event listener - change 'clicks' to 'click' to enable button functionality. --- 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 f95d0887..78525521 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -94,7 +94,7 @@ function render() { 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 860f8f92996062fba48fc0f64cb21b4ca7957143 Mon Sep 17 00:00:00 2001 From: galyna-k Date: Fri, 1 Aug 2025 14:36:33 +0200 Subject: [PATCH 05/12] fix logic for displaying read/unread status - show "Yes" when book is marked as read. --- 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 78525521..12192926 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -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"; From a0ac705446cc61d2cd8f31a4dde50e8e546575ba Mon Sep 17 00:00:00 2001 From: galyna-k Date: Fri, 1 Aug 2025 15:51:36 +0200 Subject: [PATCH 06/12] refactor: improve book form and fix input types MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Set correct to "My Book Library" - Replace <div> with <form id="book-form"> for proper submission handling - Fix incorrect input types: "title" and "author" → "text" - Set checkbox value to "read" - Remove inline onClick handler from submit button - Clean up initial <tbody> structure to avoid empty table row --- debugging/book-library/index.html | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 23acfa71..8287a0d0 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,7 +1,7 @@ <!DOCTYPE html> <html> <head> - <title> + My Book Library Library
-
+
Library /> Library type="checkbox" class="form-check-input" id="check" - value="" + value="read" />Read -
+
@@ -81,13 +80,6 @@

Library

- - - - - - -
From 74cbb72fe52d40aadcdce6746ae9993dc762ef44 Mon Sep 17 00:00:00 2001 From: galyna-k Date: Fri, 1 Aug 2025 15:56:14 +0200 Subject: [PATCH 07/12] refactor: clean up form submission logic and improve data handling MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Fix typo: "Robison Crusoe" → "Robinson Crusoe" - Remove duplicate render() call after initial data push - Replace inline form submission with `form.addEventListener('submit', ...)` - Rename submit() → submitNewBook() for clarity - Ensure pages are stored as a number in Book constructor - Replace innerHTML with textContent for safer rendering - Replace `==` with strict `===` for checkbox comparison - Add `form.reset()` after successful book submission --- debugging/book-library/script.js | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 12192926..9c15787f 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -7,7 +7,7 @@ window.addEventListener("load", function (e) { function populateStorage() { if (myLibrary.length == 0) { - let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true); + let book1 = new Book("Robinson Crusoe", "Daniel Defoe", "252", true); let book2 = new Book( "The Old Man and the Sea", "Ernest Hemingway", @@ -16,10 +16,10 @@ function populateStorage() { ); myLibrary.push(book1); myLibrary.push(book2); - render(); } } +const form = document.getElementById("book-form"); const title = document.getElementById("title"); const author = document.getElementById("author"); const pages = document.getElementById("pages"); @@ -27,12 +27,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() { +function submitNewBook() { if ( - title.value == null || - title.value == "" || - pages.value == null || - pages.value == "" + title.value === null || + title.value === "" || + pages.value === null || + pages.value === "" ) { alert("Please fill all fields!"); return false; @@ -40,13 +40,19 @@ function submit() { let book = new Book(title.value, author.value, pages.value, check.checked); myLibrary.push(book); render(); + form.reset(); } } +form.addEventListener('submit', function(event) { + event.preventDefault(); + submitNewBook(); +}); + function Book(title, author, pages, check) { this.title = title; this.author = author; - this.pages = pages; + this.pages = Number(pages); this.check = check; } @@ -66,8 +72,8 @@ 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; + titleCell.textContent = myLibrary[i].title; + authorCell.textContent = myLibrary[i].author; pagesCell.innerHTML = myLibrary[i].pages; //add and wait for action for read/unread button @@ -76,7 +82,7 @@ function render() { changeBut.className = "btn btn-success"; wasReadCell.appendChild(changeBut); let readStatus = ""; - if (myLibrary[i].check == true) { + if (myLibrary[i].check === true) { readStatus = "Yes"; } else { readStatus = "No"; From ec63ddf8d5086d9578a524b97bd3372b6d17c5da Mon Sep 17 00:00:00 2001 From: galyna-k Date: Fri, 8 Aug 2025 17:09:28 +0100 Subject: [PATCH 08/12] fix: make index.html W3C-valid HTML --- debugging/book-library/index.html | 53 ++++++------------------------- 1 file changed, 10 insertions(+), 43 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 8287a0d0..a097deef 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,20 +1,14 @@ - + My Book Library - + + - - + + @@ -30,42 +24,15 @@

Library

- + - + - + - +
From f2afb9f421c68208adc046c0752ed2fa1aa6c632 Mon Sep 17 00:00:00 2001 From: galyna-k Date: Fri, 8 Aug 2025 17:50:08 +0100 Subject: [PATCH 09/12] refactor: improve book table rendering, validation, and naming MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - replace constructor function with ES6 class for Book; - rename variables ('check' → 'isRead') for semantic clarity; - improve input validation logic: trims input, ensures page count is a valid number > 0; - remove unnecessary else block after validation return; - clean up event listener and DOM variable naming for better readability; - reorganize button creation with modern const declarations; - ensure proper UX: confirmation message shown after deletion, not before; - use textContent consistently instead of innerHTML; - remove unnecessary IDs and used button text instead; - optimize table row clearing using a while loop. --- debugging/book-library/script.js | 121 +++++++++++++------------------ 1 file changed, 51 insertions(+), 70 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 9c15787f..773d3d74 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,47 +1,40 @@ let myLibrary = []; -window.addEventListener("load", function (e) { +window.addEventListener("load", function () { populateStorage(); - render(); }); function populateStorage() { if (myLibrary.length == 0) { - let book1 = new Book("Robinson Crusoe", "Daniel Defoe", "252", true); - let book2 = new Book( - "The Old Man and the Sea", - "Ernest Hemingway", - "127", - true - ); - myLibrary.push(book1); - myLibrary.push(book2); + const book1 = new Book("Robinson Crusoe", "Daniel Defoe", 252, true); + const book2 = new Book("The Old Man and the Sea", "Ernest Hemingway", 127, true); + myLibrary.push(book1,book2); } + render(); } const form = document.getElementById("book-form"); -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 checkReadStatus = 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 submitNewBook() { - if ( - title.value === null || - title.value === "" || - pages.value === null || - pages.value === "" - ) { - alert("Please fill all fields!"); + let title = titleInput.value.trim(); + let author = authorInput.value.trim(); + let pages = Number(pagesInput.value.trim()); + let isRead = checkReadStatus.checked; + + if (!title || !author || !pages || isNaN(pages) || pages <= 0) { + alert("Please fill all fields correctly!"); return false; - } else { - let book = new Book(title.value, author.value, pages.value, check.checked); - myLibrary.push(book); - render(); - form.reset(); } + const book = new Book(title, author, pages, isRead); + myLibrary.push(book); + render(); + form.reset(); } form.addEventListener('submit', function(event) { @@ -49,61 +42,49 @@ form.addEventListener('submit', function(event) { submitNewBook(); }); -function Book(title, author, pages, check) { - this.title = title; - this.author = author; - this.pages = Number(pages); - this.check = check; +class Book { + constructor(title, author, pages, isRead) { + this.title = title; + this.author = author; + this.pages = Number(pages); + this.isRead = isRead; + } } function render() { - let table = document.getElementById("display"); - let rowsNumber = table.rows.length; + const table = document.getElementById("display"); //delete old table - for (let n = rowsNumber - 1; n > 0; n--) { - table.deleteRow(n); + while (table.rows.length > 1) { + table.deleteRow(1); } //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.textContent = myLibrary[i].title; - authorCell.textContent = myLibrary[i].author; - pagesCell.innerHTML = myLibrary[i].pages; + myLibrary.forEach((book, index) => { + const row = table.insertRow(1); + row.insertCell(0).textContent = book.title; + row.insertCell(1).textContent = book.author; + row.insertCell(2).textContent = book.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 () { - myLibrary[i].check = !myLibrary[i].check; + const wasReadCell = row.insertCell(3); + const toggleBtn = document.createElement("button"); + toggleBtn.textContent = book.isRead ? "Yes" : "No"; + toggleBtn.className = "btn btn-success"; + toggleBtn.addEventListener("click", function () { + book.isRead = !book.isRead; render(); }); + wasReadCell.appendChild(toggleBtn); //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}`); - myLibrary.splice(i, 1); + const deleteCell = row.insertCell(4); + const deleteBtn = document.createElement("button"); + deleteBtn.textContent = "Delete"; + deleteBtn.className = "btn btn-warning"; + deleteBtn.addEventListener("click", function () { + myLibrary.splice(index, 1); render(); + alert(`You've deleted book with title: ${book.title}`); }); - } + deleteCell.appendChild(deleteBtn); + }); } From e1d25813c0f6595ea41fc94a18a8edc57e31bffd Mon Sep 17 00:00:00 2001 From: galyna-k Date: Fri, 8 Aug 2025 22:40:35 +0100 Subject: [PATCH 10/12] add validation to ensure the pages input is a positive integer --- 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 773d3d74..02543b66 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -27,7 +27,7 @@ function submitNewBook() { let pages = Number(pagesInput.value.trim()); let isRead = checkReadStatus.checked; - if (!title || !author || !pages || isNaN(pages) || pages <= 0) { + if (!title || !author || !pages || !Number.isInteger(pages) || pages <= 0) { alert("Please fill all fields correctly!"); return false; } From eeeeaee0404023daa2b3e9555eccc7d1a23ad58b Mon Sep 17 00:00:00 2001 From: galyna-k Date: Sun, 10 Aug 2025 13:10:29 +0100 Subject: [PATCH 11/12] clear table body in one step for faster rendering --- debugging/book-library/script.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 02543b66..593dd108 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -53,10 +53,9 @@ class Book { function render() { const table = document.getElementById("display"); + const tableBody = table.querySelector("tbody"); //delete old table - while (table.rows.length > 1) { - table.deleteRow(1); - } + tableBody.innerHTML = ""; //insert updated row and cells myLibrary.forEach((book, index) => { const row = table.insertRow(1); From 5de7be7d7d860f4bc9a7fac9710c345f6a4a476c Mon Sep 17 00:00:00 2001 From: galyna-k Date: Sun, 10 Aug 2025 13:58:04 +0100 Subject: [PATCH 12/12] Refactor render() to centralize cell creation and modularize button setup - create all cells in one place within render(); - extract read toggle and delete button creation to helper functions; - improve code readability and maintainability --- debugging/book-library/script.js | 61 +++++++++++++++++++------------- 1 file changed, 37 insertions(+), 24 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 593dd108..fe0ef75e 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -42,6 +42,29 @@ form.addEventListener('submit', function(event) { submitNewBook(); }); +function createReadToggleButton(cell, book) { + const toggleBtn = document.createElement("button"); + toggleBtn.textContent = book.isRead ? "Yes" : "No"; + toggleBtn.className = "btn btn-success"; + toggleBtn.addEventListener("click", function () { + book.isRead = !book.isRead; + render(); + }); + cell.appendChild(toggleBtn); +} + +function createDeleteButton(cell, book, index) { + const deleteBtn = document.createElement("button"); + deleteBtn.textContent = "Delete"; + deleteBtn.className = "btn btn-warning"; + deleteBtn.addEventListener("click", function () { + myLibrary.splice(index, 1); + render(); + alert(`You've deleted book with title: ${book.title}`); + }); + cell.appendChild(deleteBtn); +} + class Book { constructor(title, author, pages, isRead) { this.title = title; @@ -55,35 +78,25 @@ function render() { const table = document.getElementById("display"); const tableBody = table.querySelector("tbody"); //delete old table - tableBody.innerHTML = ""; + tableBody.innerHTML = ""; //insert updated row and cells myLibrary.forEach((book, index) => { - const row = table.insertRow(1); - row.insertCell(0).textContent = book.title; - row.insertCell(1).textContent = book.author; - row.insertCell(2).textContent = book.pages; + const row = tableBody.insertRow(0); + + const titleCell = row.insertCell(0); + const authorCell = row.insertCell(1); + const pagesCell = row.insertCell(2); + const readCell = row.insertCell(3); + const deleteCell = row.insertCell(4); + + titleCell.textContent = book.title; + authorCell.textContent = book.author; + pagesCell.textContent = book.pages; //add and wait for action for read/unread button - const wasReadCell = row.insertCell(3); - const toggleBtn = document.createElement("button"); - toggleBtn.textContent = book.isRead ? "Yes" : "No"; - toggleBtn.className = "btn btn-success"; - toggleBtn.addEventListener("click", function () { - book.isRead = !book.isRead; - render(); - }); - wasReadCell.appendChild(toggleBtn); + createReadToggleButton(readCell, book); //add delete button to every row and render again - const deleteCell = row.insertCell(4); - const deleteBtn = document.createElement("button"); - deleteBtn.textContent = "Delete"; - deleteBtn.className = "btn btn-warning"; - deleteBtn.addEventListener("click", function () { - myLibrary.splice(index, 1); - render(); - alert(`You've deleted book with title: ${book.title}`); - }); - deleteCell.appendChild(deleteBtn); + createDeleteButton(deleteCell, book, index); }); }