From 1c0448549f8d4711e9aef97b46f6fe17c049e3c7 Mon Sep 17 00:00:00 2001 From: Pretty Taruvinga Date: Wed, 22 Apr 2026 14:58:43 +0200 Subject: [PATCH 1/5] added code for fixes and changes for the book library task --- debugging/book-library/index.html | 19 ++--- debugging/book-library/script.js | 130 +++++++++++++++--------------- 2 files changed, 71 insertions(+), 78 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 23acfa71..b9f1e43b 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,12 +1,9 @@ - - + + - - + My Book Library + + @@ -31,7 +28,7 @@

Library

Library /> Library type="submit" value="Submit" class="btn btn-primary" - onclick="submit();" + onclick="submit()" />
diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 75ce6c1d..c1a3cced 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,46 +1,45 @@ let myLibrary = []; -window.addEventListener("load", function (e) { +window.addEventListener("load", function () { populateStorage(); - render(); + render(); // only called once now }); 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("Robison 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(); + + myLibrary.push(book1, book2); + + // removed extra render() here } } -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"); -//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); + + if (!title || !author || !pages || pages <= 0) { + alert("Please fill all fields correctly!"); + return; } + + const book = new Book(title, author, pages, readCheckbox.checked); + + myLibrary.push(book); + render(); } function Book(title, author, pages, check) { @@ -51,53 +50,50 @@ 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); + const table = document.getElementById("display"); + + // remove old rows (keep header row) + 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.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 = "Yes"; - } else { - readStatus = "No"; - } - changeBut.innerText = readStatus; - - changeBut.addEventListener("click", function () { + + for (let i = 0; i < myLibrary.length; i++) { + 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; + + const toggleButton = document.createElement("button"); + toggleButton.className = "btn btn-success"; + toggleButton.textContent = myLibrary[i].check ? "Yes" : "No"; + + toggleButton.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}`); + wasReadCell.appendChild(toggleButton); + + const deleteButton = document.createElement("button"); + deleteButton.className = "btn btn-warning"; + deleteButton.textContent = "Delete"; + + deleteButton.addEventListener("click", function () { + const deletedTitle = myLibrary[i].title; + myLibrary.splice(i, 1); render(); + + alert(`You've deleted title: ${deletedTitle}`); }); + + deleteCell.appendChild(deleteButton); } } From 7bae570b48913d9030471a19843b30fc887ca25e Mon Sep 17 00:00:00 2001 From: Pretty Taruvinga Date: Fri, 24 Apr 2026 00:03:43 +0200 Subject: [PATCH 2/5] Use const for myLibrary to prevent reassignment --- 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 c1a3cced..c4da41d5 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,4 +1,4 @@ -let myLibrary = []; +const myLibrary = []; window.addEventListener("load", function () { populateStorage(); From 1001291f294bcf09fbb5556e9a3a7fe743b8343e Mon Sep 17 00:00:00 2001 From: Pretty Taruvinga Date: Fri, 24 Apr 2026 00:09:20 +0200 Subject: [PATCH 3/5] Move DOM element references to top of file --- debugging/book-library/script.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index c4da41d5..17e752fe 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,4 +1,8 @@ const myLibrary = []; +const titleInput = document.getElementById("title"); +const authorInput = document.getElementById("author"); +const pagesInput = document.getElementById("pages"); +const readCheckbox = document.getElementById("check"); window.addEventListener("load", function () { populateStorage(); @@ -21,11 +25,6 @@ function populateStorage() { } } -const titleInput = document.getElementById("title"); -const authorInput = document.getElementById("author"); -const pagesInput = document.getElementById("pages"); -const readCheckbox = document.getElementById("check"); - function submit() { const title = titleInput.value.trim(); const author = authorInput.value.trim(); From f4b300f53675a5719fbd4c71d19c95ccf04fd577 Mon Sep 17 00:00:00 2001 From: Pretty Taruvinga Date: Fri, 24 Apr 2026 00:25:29 +0200 Subject: [PATCH 4/5] Update render to use tbody instead of table row loop Co-authored-by: Copilot --- debugging/book-library/script.js | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 17e752fe..9547bfe9 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -49,21 +49,17 @@ function Book(title, author, pages, check) { } function render() { - const table = document.getElementById("display"); - - // remove old rows (keep header row) - while (table.rows.length > 1) { - table.deleteRow(1); - } + const tbody = document.querySelector("#display tbody"); + tbody.innerHTML = ""; for (let i = 0; i < myLibrary.length; i++) { - const row = table.insertRow(1); + const row = document.createElement("tr"); - 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); + const titleCell = document.createElement("td"); + const authorCell = document.createElement("td"); + const pagesCell = document.createElement("td"); + const wasReadCell = document.createElement("td"); + const deleteCell = document.createElement("td"); titleCell.textContent = myLibrary[i].title; authorCell.textContent = myLibrary[i].author; @@ -94,5 +90,12 @@ function render() { }); deleteCell.appendChild(deleteButton); + row.appendChild(titleCell); + row.appendChild(authorCell); + row.appendChild(pagesCell); + row.appendChild(wasReadCell); + row.appendChild(deleteCell); + + tbody.appendChild(row); } } From 690923d3bf43fc73e78319d38eaabd27cb561503 Mon Sep 17 00:00:00 2001 From: Pretty Taruvinga Date: Fri, 24 Apr 2026 00:52:59 +0200 Subject: [PATCH 5/5] Simplify table rendering using insertCell and loop --- debugging/book-library/script.js | 117 +++++++++++-------------------- 1 file changed, 41 insertions(+), 76 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 9547bfe9..34826a28 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,101 +1,66 @@ const myLibrary = []; + const titleInput = document.getElementById("title"); const authorInput = document.getElementById("author"); const pagesInput = document.getElementById("pages"); const readCheckbox = document.getElementById("check"); -window.addEventListener("load", function () { - populateStorage(); - render(); // only called once now -}); - -function populateStorage() { - if (myLibrary.length === 0) { - const book1 = new Book("Robison Crusoe", "Daniel Defoe", 252, true); - const book2 = new Book( - "The Old Man and the Sea", - "Ernest Hemingway", - 127, - true - ); - - myLibrary.push(book1, book2); - - // removed extra render() here - } -} - -function submit() { - const title = titleInput.value.trim(); - const author = authorInput.value.trim(); - const pages = Number(pagesInput.value); - - if (!title || !author || !pages || pages <= 0) { - alert("Please fill all fields correctly!"); - return; - } - - const book = new Book(title, author, pages, readCheckbox.checked); - - myLibrary.push(book); - render(); -} +const tableBody = document.getElementById("table-body"); +const form = document.getElementById("book-form"); -function Book(title, author, pages, check) { +function Book(title, author, pages, wasRead) { this.title = title; this.author = author; this.pages = pages; - this.check = check; + this.wasRead = wasRead; +} + +function addBookToLibrary(title, author, pages, wasRead) { + const newBook = new Book(title, author, pages, wasRead); + myLibrary.push(newBook); } function render() { - const tbody = document.querySelector("#display tbody"); - tbody.innerHTML = ""; + tableBody.innerHTML = ""; - for (let i = 0; i < myLibrary.length; i++) { - const row = document.createElement("tr"); + myLibrary.forEach((book, index) => { + const row = tableBody.insertRow(); - const titleCell = document.createElement("td"); - const authorCell = document.createElement("td"); - const pagesCell = document.createElement("td"); - const wasReadCell = document.createElement("td"); - const deleteCell = document.createElement("td"); + 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 = myLibrary[i].title; - authorCell.textContent = myLibrary[i].author; - pagesCell.textContent = myLibrary[i].pages; + titleCell.textContent = book.title; + authorCell.textContent = book.author; + pagesCell.textContent = book.pages; + readCell.textContent = book.wasRead ? "Read" : "Not Read"; - const toggleButton = document.createElement("button"); - toggleButton.className = "btn btn-success"; - toggleButton.textContent = myLibrary[i].check ? "Yes" : "No"; + const deleteBtn = document.createElement("button"); + deleteBtn.textContent = "Delete"; - toggleButton.addEventListener("click", function () { - myLibrary[i].check = !myLibrary[i].check; + deleteBtn.addEventListener("click", () => { + myLibrary.splice(index, 1); render(); }); - wasReadCell.appendChild(toggleButton); - - const deleteButton = document.createElement("button"); - deleteButton.className = "btn btn-warning"; - deleteButton.textContent = "Delete"; + deleteCell.appendChild(deleteBtn); + }); +} - deleteButton.addEventListener("click", function () { - const deletedTitle = myLibrary[i].title; +form.addEventListener("submit", (e) => { + e.preventDefault(); - myLibrary.splice(i, 1); - render(); + addBookToLibrary( + titleInput.value, + authorInput.value, + pagesInput.value, + readCheckbox.checked + ); - alert(`You've deleted title: ${deletedTitle}`); - }); - - deleteCell.appendChild(deleteButton); - row.appendChild(titleCell); - row.appendChild(authorCell); - row.appendChild(pagesCell); - row.appendChild(wasReadCell); - row.appendChild(deleteCell); + form.reset(); + render(); +}); - tbody.appendChild(row); - } -} +render();