From 4136ae2f13f6cc90faf5414605cff0be689b88cb Mon Sep 17 00:00:00 2001 From: Konvaly Date: Tue, 16 Dec 2025 21:55:50 +0000 Subject: [PATCH 01/10] Fix syntax error in render loop preventing initial render --- 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 478a62d547b6a4b64176b756572660e58d172d91 Mon Sep 17 00:00:00 2001 From: Konvaly Date: Tue, 16 Dec 2025 22:01:15 +0000 Subject: [PATCH 02/10] Fix delete button variable name causing render crash --- 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 eed706fd..abcd4293 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -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 4843708d352e903a1a7301887aa3a8f80461a275 Mon Sep 17 00:00:00 2001 From: Konvaly Date: Tue, 16 Dec 2025 22:13:42 +0000 Subject: [PATCH 03/10] Fix author field when creating new Book from form input --- 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 abcd4293..f95d0887 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -37,8 +37,8 @@ function submit() { 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(); } } From ab5513e3c8ef0f8b1d642d63e25512c8914a1157 Mon Sep 17 00:00:00 2001 From: Konvaly Date: Tue, 16 Dec 2025 22:17:28 +0000 Subject: [PATCH 04/10] Fix read status logic when rendering books --- 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 f95d0887..ec931adf 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -77,9 +77,9 @@ function render() { wasReadCell.appendChild(changeBut); let readStatus = ""; if (myLibrary[i].check == false) { - readStatus = "Yes"; - } else { readStatus = "No"; + } else { + readStatus = "Yes"; } changeBut.innerText = readStatus; From 1fb857774746603cac3babbffc057f5ac1d4ff57 Mon Sep 17 00:00:00 2001 From: Konvaly Date: Tue, 16 Dec 2025 22:19:54 +0000 Subject: [PATCH 05/10] Fix delete button by using correct click event listener --- 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 ec931adf..098c74ec 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 610f0ce9a4f7c98e4e4d508847b438591544ed36 Mon Sep 17 00:00:00 2001 From: Konvaly Date: Tue, 16 Dec 2025 23:34:58 +0000 Subject: [PATCH 06/10] Clear form fields after successful book submission --- debugging/book-library/script.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 098c74ec..b800834b 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -40,6 +40,12 @@ function submit() { let book = new Book(title.value, author.value, pages.value, check.checked); myLibrary.push(book); render(); + + //clear the form after submit + title.value = ""; + author.value = ""; + pages.value = ""; + check.checked = false; } } From 6acbd7c58634f06dd1a691c91a54c1a177f5d1ac Mon Sep 17 00:00:00 2001 From: Konvaly Date: Tue, 16 Dec 2025 23:42:37 +0000 Subject: [PATCH 07/10] Prevent adding duplicate books by title --- debugging/book-library/script.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index b800834b..5032b51d 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -37,17 +37,24 @@ function submit() { alert("Please fill all fields!"); return false; } else { + const alreadyExists = myLibrary.some( + (book) => book.title === title.value); + if (alreadyExists) { + alert("This book is already in your library!"); + return; + } + } let book = new Book(title.value, author.value, pages.value, check.checked); myLibrary.push(book); render(); - + //clear the form after submit title.value = ""; author.value = ""; pages.value = ""; check.checked = false; } -} + function Book(title, author, pages, check) { this.title = title; From 7168d78b5622413444eb3837d640ea2130320f7a Mon Sep 17 00:00:00 2001 From: Konvaly Date: Thu, 18 Dec 2025 03:26:51 +0000 Subject: [PATCH 08/10] Improve book form validation and render rows in tbody --- debugging/book-library/index.html | 136 +++++++++++------------------- debugging/book-library/script.js | 125 +++++++++++++-------------- 2 files changed, 107 insertions(+), 154 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 23acfa71..585defc7 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,96 +1,56 @@ - - - - - - - - - - -
-

Library

-

Add books to your virtual library

-
+ + + + + + + + + + + + +
+

Library

+

Add books to your virtual library

+
- + -
-
- - - - - - - - -
+
+
+ + + + + + + +
+
+ + + + + + + + + + + + +
TitleAuthorNumber of PagesRead
- - - - - - - - - - - - - - - - - - - -
TitleAuthorNumber of PagesRead
+ + - - - + \ No newline at end of file diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 5032b51d..85562fd1 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -7,16 +7,15 @@ 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", - "127", + 127, true ); myLibrary.push(book1); myLibrary.push(book2); - render(); } } @@ -25,36 +24,39 @@ 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 == "" - ) { + const titleValue = title.value.trim(); + const authorValue = author.value.trim(); + const pagesValue = pages.value.trim(); + const pagesNumber = Number(pagesValue); + + if (!titleValue || !authorValue || !pagesValue) { alert("Please fill all fields!"); - return false; - } else { - const alreadyExists = myLibrary.some( - (book) => book.title === title.value); - if (alreadyExists) { - alert("This book is already in your library!"); - return; - } + return; } - let book = new Book(title.value, author.value, pages.value, check.checked); - myLibrary.push(book); - render(); - - //clear the form after submit - title.value = ""; - author.value = ""; - pages.value = ""; - check.checked = false; + + if (!Number.isFinite(pagesNumber) || pagesNumber <= 0) { + alert("Pages must be a positive number."); + return; } + const alreadyExists = myLibrary.some( + (book) => book.title.toLowerCase() === titleValue.toLowerCase() + ); + if (alreadyExists) { + alert("This book is already in your library!"); + return; + } + + let book = new Book(titleValue, authorValue, pagesNumber, check.checked); + myLibrary.push(book); + render(); + + title.value = ""; + author.value = ""; + pages.value = ""; + check.checked = false; +} function Book(title, author, pages, check) { this.title = title; @@ -64,53 +66,44 @@ 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 == false) { - readStatus = "No"; - } else { - readStatus = "Yes"; - } - changeBut.innerText = readStatus; + const tbody = document.getElementById("bookRows"); + tbody.textContent = ""; + for (let i = 0; i < myLibrary.length; i++) { + const book = myLibrary[i]; + + 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 = book.title; + authorCell.textContent = book.author; + pagesCell.textContent = book.pages; + + const changeBut = document.createElement("button"); + changeBut.className = "btn btn-success"; + changeBut.textContent = book.check ? "Yes" : "No"; changeBut.addEventListener("click", function () { - myLibrary[i].check = !myLibrary[i].check; + book.check = !book.check; render(); }); + wasReadCell.appendChild(changeBut); - //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.textContent = "Delete"; delBut.addEventListener("click", function () { - alert(`You've deleted title: ${myLibrary[i].title}`); - myLibrary.splice(i, 1); + const bookIndex = myLibrary.indexOf(book); + if (bookIndex === -1) return; + + myLibrary.splice(bookIndex, 1); render(); + alert(`You've deleted title: ${book.title}`); }); + deleteCell.appendChild(delBut); } } From 83183be03cced78d8e181e4856285494a29b7291 Mon Sep 17 00:00:00 2001 From: Konvaly Date: Thu, 18 Dec 2025 03:37:15 +0000 Subject: [PATCH 09/10] Fixed index.html according to validator --- debugging/book-library/index.html | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 585defc7..db15d6fc 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,15 +1,15 @@ - + - - - + Book Library + + - - + + @@ -25,15 +25,15 @@

Library

- + - + - + - +
From b2536c56fc819fc4395087421b41574b67f51138 Mon Sep 17 00:00:00 2001 From: Konvaly Date: Fri, 19 Dec 2025 02:44:52 +0000 Subject: [PATCH 10/10] Refactor according to mentor's review --- debugging/book-library/script.js | 33 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 85562fd1..39870583 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -25,12 +25,11 @@ const pages = document.getElementById("pages"); const check = document.getElementById("check"); function submit() { - const titleValue = title.value.trim(); - const authorValue = author.value.trim(); - const pagesValue = pages.value.trim(); - const pagesNumber = Number(pagesValue); + const titleText = title.value.trim(); + const authorText = author.value.trim(); + const pagesNumber = Number(pages.value.trim()); - if (!titleValue || !authorValue || !pagesValue) { + if (!titleText || !authorText || !pages.value.trim()) { alert("Please fill all fields!"); return; } @@ -41,14 +40,14 @@ function submit() { } const alreadyExists = myLibrary.some( - (book) => book.title.toLowerCase() === titleValue.toLowerCase() + (book) => book.title.toLowerCase() === titleText.toLowerCase() ); if (alreadyExists) { alert("This book is already in your library!"); return; } - let book = new Book(titleValue, authorValue, pagesNumber, check.checked); + const book = new Book(titleText, authorText, pagesNumber, check.checked); myLibrary.push(book); render(); @@ -84,19 +83,19 @@ function render() { authorCell.textContent = book.author; pagesCell.textContent = book.pages; - const changeBut = document.createElement("button"); - changeBut.className = "btn btn-success"; - changeBut.textContent = book.check ? "Yes" : "No"; - changeBut.addEventListener("click", function () { + const changeBtn = document.createElement("button"); + changeBtn.className = "btn btn-success"; + changeBtn.textContent = book.check ? "Yes" : "No"; + changeBtn.addEventListener("click", function () { book.check = !book.check; render(); }); - wasReadCell.appendChild(changeBut); + wasReadCell.appendChild(changeBtn); - let delBut = document.createElement("button"); - delBut.className = "btn btn-warning"; - delBut.textContent = "Delete"; - delBut.addEventListener("click", function () { + let delBtn = document.createElement("button"); + delBtn.className = "btn btn-warning"; + delBtn.textContent = "Delete"; + delBtn.addEventListener("click", function () { const bookIndex = myLibrary.indexOf(book); if (bookIndex === -1) return; @@ -104,6 +103,6 @@ function render() { render(); alert(`You've deleted title: ${book.title}`); }); - deleteCell.appendChild(delBut); + deleteCell.appendChild(delBtn); } }