From 03ffcc7316938209623d96aada7eeb5a3d7b17f9 Mon Sep 17 00:00:00 2001 From: Chi <181338252+Chi-Mb@users.noreply.github.com> Date: Thu, 15 May 2025 13:15:12 +0100 Subject: [PATCH 1/9] Updated HTML file --- debugging/book-library/index.html | 119 ++++++++++++------------------ 1 file changed, 46 insertions(+), 73 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 23acfa71..d4ec375b 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,96 +1,69 @@ - - - - - - - - - - + + + + + Chi's Book Library + + + + + + + + + + - -
-

Library

-

Add books to your virtual library

-
+ +
+

Library

+

Add books to your virtual library

+
- -
-
+
+
- - - + + + + + - - - -
+ + +
+ + +
+ + +
- +
- + - - - - - - - +
Title Author Number of Pages ReadActions
+
- - + + From ec7b5813f0fbdffb9190cd5e70eadf9a32161774 Mon Sep 17 00:00:00 2001 From: Chi <181338252+Chi-Mb@users.noreply.github.com> Date: Thu, 15 May 2025 13:16:26 +0100 Subject: [PATCH 2/9] Fixed errors in the JS file --- debugging/book-library/script.js | 58 ++++++++++++-------------------- 1 file changed, 21 insertions(+), 37 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 75ce6c1d..4818c4cf 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,22 +1,17 @@ let myLibrary = []; -window.addEventListener("load", function (e) { +window.addEventListener("load", function () { populateStorage(); render(); + document.getElementById("submitBook").addEventListener("click", submit); }); 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 - ); + 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); - render(); } } @@ -25,20 +20,17 @@ 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 == "" + title.value.trim() === "" || + author.value.trim() === "" || + pages.value.trim() === "" ) { 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(); } } @@ -53,51 +45,43 @@ 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-- { + + + 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++) { + + for (let i = 0; i < myLibrary.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.innerText = myLibrary[i].check ? "Yes" : "No"; changeBut.addEventListener("click", function () { myLibrary[i].check = !myLibrary[i].check; render(); }); + wasReadCell.appendChild(changeBut); - //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"; delBut.innerHTML = "Delete"; - delBut.addEventListener("clicks", function () { + delBut.addEventListener("click", function () { alert(`You've deleted title: ${myLibrary[i].title}`); myLibrary.splice(i, 1); render(); }); + deleteCell.appendChild(delBut); } } From a08233e2ca7f32e0e148a4638e67ee32932d5cf6 Mon Sep 17 00:00:00 2001 From: Chi <181338252+Chi-Mb@users.noreply.github.com> Date: Mon, 19 May 2025 12:59:36 +0100 Subject: [PATCH 3/9] Added CSS file for full functionality --- debugging/book-library/style.css | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/debugging/book-library/style.css b/debugging/book-library/style.css index 302950cb..e116e3f9 100644 --- a/debugging/book-library/style.css +++ b/debugging/book-library/style.css @@ -1,6 +1,6 @@ .form-group { width: 400px; - height: 300px; + height: 310px; align-self: left; padding-left: 20px; } From dc20c4ce5cc5084211c0335ee3fb0ab68f4a4f0e Mon Sep 17 00:00:00 2001 From: Chi <181338252+Chi-Mb@users.noreply.github.com> Date: Mon, 19 May 2025 13:00:44 +0100 Subject: [PATCH 4/9] Update style.css --- debugging/book-library/style.css | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/debugging/book-library/style.css b/debugging/book-library/style.css index e116e3f9..302950cb 100644 --- a/debugging/book-library/style.css +++ b/debugging/book-library/style.css @@ -1,6 +1,6 @@ .form-group { width: 400px; - height: 310px; + height: 300px; align-self: left; padding-left: 20px; } From 3e21541397d539d51eaa48ec0f96bffde5868a08 Mon Sep 17 00:00:00 2001 From: Chi <181338252+Chi-Mb@users.noreply.github.com> Date: Mon, 19 May 2025 13:03:21 +0100 Subject: [PATCH 5/9] Updated CSS --- debugging/book-library/style.css | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/debugging/book-library/style.css b/debugging/book-library/style.css index 302950cb..e06e5484 100644 --- a/debugging/book-library/style.css +++ b/debugging/book-library/style.css @@ -1,6 +1,6 @@ .form-group { width: 400px; - height: 300px; + height: 305px; align-self: left; padding-left: 20px; } From 8118c5c13903da492e42f44daf82e59aa40a547f Mon Sep 17 00:00:00 2001 From: Chi <181338252+Chi-Mb@users.noreply.github.com> Date: Mon, 19 May 2025 14:01:47 +0100 Subject: [PATCH 6/9] Update script.js --- debugging/book-library/script.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 4818c4cf..dc0edd1a 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -47,9 +47,10 @@ function render() { let rowsNumber = table.rows.length; - for (let n = rowsNumber - 1; n > 0; n--) { - table.deleteRow(n); - } + while (table.rows.length > 1) { + table.deleteRow(1); +} + for (let i = 0; i < myLibrary.length; i++) { let row = table.insertRow(1); From 3af93884080090d2a6c0b08dd5b665913b1feda8 Mon Sep 17 00:00:00 2001 From: Chi <181338252+Chi-Mb@users.noreply.github.com> Date: Mon, 19 May 2025 14:26:19 +0100 Subject: [PATCH 7/9] Add isNaN and check for pages > 0 to ensure valid input --- debugging/book-library/script.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index dc0edd1a..c75a8663 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -24,7 +24,9 @@ function submit() { if ( title.value.trim() === "" || author.value.trim() === "" || - pages.value.trim() === "" + pages.value.trim() === "" || + isNaN(pages.value.trim()) || + Number(pages.value.trim()) <= 0 ) { alert("Please fill all fields!"); return false; From b9a8cd785fd5d21f8e6d8f7ce32c423bd5c186b8 Mon Sep 17 00:00:00 2001 From: Chi <181338252+Chi-Mb@users.noreply.github.com> Date: Mon, 19 May 2025 22:36:43 +0100 Subject: [PATCH 8/9] Refactored render to replace tbody --- debugging/book-library/script.js | 57 +++++++++++++++++++------------- 1 file changed, 34 insertions(+), 23 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index c75a8663..6f0e8573 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -24,9 +24,7 @@ function submit() { if ( title.value.trim() === "" || author.value.trim() === "" || - pages.value.trim() === "" || - isNaN(pages.value.trim()) || - Number(pages.value.trim()) <= 0 + pages.value.trim() === "" ) { alert("Please fill all fields!"); return false; @@ -45,28 +43,29 @@ function Book(title, author, pages, check) { } function render() { - let table = document.getElementById("display"); - let rowsNumber = table.rows.length; + const table = document.getElementById("display"); + const oldTbody = table.querySelector("tbody"); - - while (table.rows.length > 1) { - table.deleteRow(1); -} + if (oldTbody) { + table.removeChild(oldTbody); + } + const newTbody = document.createElement("tbody"); for (let i = 0; i < myLibrary.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; - - let changeBut = document.createElement("button"); + const row = document.createElement("tr"); + + const titleCell = document.createElement("td"); + titleCell.textContent = myLibrary[i].title; + + const authorCell = document.createElement("td"); + authorCell.textContent = myLibrary[i].author; + + const pagesCell = document.createElement("td"); + pagesCell.textContent = myLibrary[i].pages; + + const wasReadCell = document.createElement("td"); + const changeBut = document.createElement("button"); changeBut.id = i; changeBut.className = "btn btn-success"; changeBut.innerText = myLibrary[i].check ? "Yes" : "No"; @@ -76,15 +75,27 @@ function render() { }); wasReadCell.appendChild(changeBut); - let delBut = document.createElement("button"); + const deleteCell = document.createElement("td"); + const delBut = document.createElement("button"); delBut.id = i + 5; delBut.className = "btn btn-warning"; - delBut.innerHTML = "Delete"; + delBut.innerText = "Delete"; delBut.addEventListener("click", function () { alert(`You've deleted title: ${myLibrary[i].title}`); myLibrary.splice(i, 1); render(); }); deleteCell.appendChild(delBut); + + row.appendChild(titleCell); + row.appendChild(authorCell); + row.appendChild(pagesCell); + row.appendChild(wasReadCell); + row.appendChild(deleteCell); + + newTbody.appendChild(row); } + + table.appendChild(newTbody); } + From 14c52629c83127b7154c58f692d0a30e15dc424a Mon Sep 17 00:00:00 2001 From: Chi <181338252+Chi-Mb@users.noreply.github.com> Date: Mon, 19 May 2025 23:05:20 +0100 Subject: [PATCH 9/9] Improved naming and clean up button IDs --- debugging/book-library/script.js | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 6f0e8573..a623277f 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -15,21 +15,21 @@ function populateStorage() { } } -const title = document.getElementById("title"); -const author = document.getElementById("author"); -const pages = document.getElementById("pages"); -const check = document.getElementById("check"); +const titleInputEl = document.getElementById("title"); +const authorInputEl = document.getElementById("author"); +const pagesInputEl = document.getElementById("pages"); +const checkInputEl = document.getElementById("check"); function submit() { if ( - title.value.trim() === "" || - author.value.trim() === "" || - pages.value.trim() === "" + titleInputEl.value.trim() === "" || + authorInputEl.value.trim() === "" || + pagesInputEl.value.trim() === "" ) { alert("Please fill all fields!"); return false; } else { - let book = new Book(title.value, author.value, pages.value, check.checked); + let book = new Book(titleInputEl.value, authorInputEl.value, pagesInputEl.value, checkInputEl.checked); myLibrary.push(book); render(); } @@ -66,7 +66,6 @@ function render() { const wasReadCell = document.createElement("td"); const changeBut = document.createElement("button"); - changeBut.id = i; changeBut.className = "btn btn-success"; changeBut.innerText = myLibrary[i].check ? "Yes" : "No"; changeBut.addEventListener("click", function () { @@ -77,7 +76,6 @@ function render() { const deleteCell = document.createElement("td"); const delBut = document.createElement("button"); - delBut.id = i + 5; delBut.className = "btn btn-warning"; delBut.innerText = "Delete"; delBut.addEventListener("click", function () {