From 4ac3b8721e8d28be249e42c6a9827c1806db9473 Mon Sep 17 00:00:00 2001 From: KayanatSuleman Date: Fri, 10 Apr 2026 18:56:49 +0100 Subject: [PATCH 1/9] Bug fix for loop --- 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 59f56c0805a01cbb84704ab9be8b439346eb0ca5 Mon Sep 17 00:00:00 2001 From: KayanatSuleman Date: Fri, 10 Apr 2026 18:58:53 +0100 Subject: [PATCH 2/9] Bug fix incorrect variable fix --- 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..51a8c1cb 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(); } } From 7e15f4ae4b30570fc4655d7d59068e7db8240c93 Mon Sep 17 00:00:00 2001 From: KayanatSuleman Date: Fri, 10 Apr 2026 19:00:40 +0100 Subject: [PATCH 3/9] Duplicate 'Title' value bug fix --- 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 51a8c1cb..45277227 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 9e99343f0d3db92250dddff261a97bef76f8c6c2 Mon Sep 17 00:00:00 2001 From: KayanatSuleman Date: Fri, 10 Apr 2026 19:02:36 +0100 Subject: [PATCH 4/9] incorrect variable references and click spelling error --- debugging/book-library/script.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 45277227..b2a942be 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -90,11 +90,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 794d74c08897862f80dc6fbe84abc60e6aacc26b Mon Sep 17 00:00:00 2001 From: KayanatSuleman Date: Fri, 10 Apr 2026 19:04:32 +0100 Subject: [PATCH 5/9] incorrect boolean bug fix --- 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 b2a942be..7e845516 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 710fda38e984a83181c6722d10f87ee07937d291 Mon Sep 17 00:00:00 2001 From: KayanatSuleman Date: Fri, 10 Apr 2026 19:07:57 +0100 Subject: [PATCH 6/9] correcting the submit function bug --- debugging/book-library/script.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 7e845516..02672a51 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -29,9 +29,8 @@ const check = document.getElementById("check"); //via Book function and start render function function submit() { if ( - title.value == null || title.value == "" || - pages.value == null || + author.value == "" || pages.value == "" ) { alert("Please fill all fields!"); From 7514acbe1cddbadc67edb029ff6ea0a04c6b5ace Mon Sep 17 00:00:00 2001 From: KayanatSuleman Date: Sat, 11 Apr 2026 13:17:48 +0100 Subject: [PATCH 7/9] Refactoring script.js --- debugging/book-library/script.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 02672a51..c8b5854d 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -2,7 +2,6 @@ let myLibrary = []; window.addEventListener("load", function (e) { populateStorage(); - render(); }); function populateStorage() { @@ -29,9 +28,9 @@ const check = document.getElementById("check"); //via Book function and start render function function submit() { if ( - title.value == "" || - author.value == "" || - pages.value == "" + title.value === "" || + author.value === "" || + pages.value === "" ) { alert("Please fill all fields!"); return false; From bd36b2ad9ff60c83ad7d73768483b466046e1b0e Mon Sep 17 00:00:00 2001 From: KayanatSuleman Date: Sat, 11 Apr 2026 13:24:59 +0100 Subject: [PATCH 8/9] correcting bug fix --- debugging/book-library/script.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index c8b5854d..82ae44aa 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -2,10 +2,11 @@ let myLibrary = []; window.addEventListener("load", function (e) { populateStorage(); + render(); }); function populateStorage() { - if (myLibrary.length == 0) { + if (myLibrary.length === 0) { let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true); let book2 = new Book( "The Old Man and the Sea", @@ -15,7 +16,6 @@ function populateStorage() { ); myLibrary.push(book1); myLibrary.push(book2); - render(); } } @@ -74,7 +74,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 18f3a987630afc96bc988d00f831f7f4a0d44bd9 Mon Sep 17 00:00:00 2001 From: KayanatSuleman Date: Sun, 12 Apr 2026 21:35:59 +0100 Subject: [PATCH 9/9] refactoring code based on review comments --- debugging/book-library/index.html | 51 +++++----- debugging/book-library/script.js | 153 ++++++++++++++++-------------- 2 files changed, 107 insertions(+), 97 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 23acfa71..01de939c 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,20 +1,19 @@ - + - - + + + Library + + - + @@ -23,7 +22,12 @@

Library

Add books to your virtual library

- @@ -31,38 +35,43 @@

Library

- + + + + + Library - - - - - - - - - + - + \ No newline at end of file diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 82ae44aa..72bb2090 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,101 +1,110 @@ -let myLibrary = []; +const 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( - "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); } } -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 displayTable = document.getElementById("display"); +const tableBody = displayTable.querySelector("tbody"); -//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 === "" - ) { - alert("Please fill all fields!"); + const titleValue = titleInput.value.trim(); + const authorValue = authorInput.value.trim(); + const pagesValue = Number(pagesInput.value); + + if (titleValue === "" || authorValue === "" || !Number.isInteger(pagesValue) || pagesValue < 1) { + 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(); } + + const book = new Book(titleValue, authorValue, pagesValue, readCheckbox.checked); + myLibrary.push(book); + + titleInput.value = ""; + authorInput.value = ""; + pagesInput.value = ""; + readCheckbox.checked = false; + + render(); + return true; } -function Book(title, author, pages, check) { +function Book(title, author, pages, isRead) { this.title = title; this.author = author; this.pages = pages; - this.check = check; + this.isRead = isRead; } 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 () { - myLibrary[i].check = !myLibrary[i].check; + tableBody.textContent = ""; + + for (const [index, book] of myLibrary.entries()) { + const row = document.createElement("tr"); + + const titleCell = document.createElement("td"); + const authorCell = document.createElement("td"); + const pagesCell = document.createElement("td"); + const readCell = document.createElement("td"); + const deleteCell = document.createElement("td"); + + titleCell.textContent = book.title; + authorCell.textContent = book.author; + pagesCell.textContent = book.pages; + + const toggleReadButton = document.createElement("button"); + toggleReadButton.className = "btn btn-success"; + toggleReadButton.textContent = book.isRead ? "Yes" : "No"; + toggleReadButton.addEventListener("click", function () { + book.isRead = !book.isRead; render(); }); - //add delete button to every row and render again - let delButton = document.createElement("button"); - 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); + const deleteButton = document.createElement("button"); + deleteButton.className = "btn btn-warning"; + deleteButton.textContent = "Delete"; + deleteButton.addEventListener("click", function () { + myLibrary.splice(index, 1); render(); + showMessage(`You've deleted title: ${book.title}`); }); + + readCell.appendChild(toggleReadButton); + deleteCell.appendChild(deleteButton); + + row.appendChild(titleCell); + row.appendChild(authorCell); + row.appendChild(pagesCell); + row.appendChild(readCell); + row.appendChild(deleteCell); + + tableBody.appendChild(row); } } + +function showMessage(message) { + let messageBox = document.getElementById("message"); + + if (!messageBox) { + messageBox = document.createElement("div"); + messageBox.id = "message"; + messageBox.className = "alert alert-info mt-3"; + displayTable.insertAdjacentElement("beforebegin", messageBox); + } + + messageBox.textContent = message; +} \ No newline at end of file