From e6be8dc89e69b09d0749f05441ccce77d568f6d9 Mon Sep 17 00:00:00 2001 From: RomanSanaye Date: Tue, 14 Apr 2026 12:38:40 +0100 Subject: [PATCH 1/3] HTML codes and Js codes debugged --- debugging/book-library/index.html | 108 +++++++++++++++--------------- debugging/book-library/script.js | 22 ++++-- debugging/book-library/style.css | 48 +++++++++++-- 3 files changed, 109 insertions(+), 69 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 23acfa71..97ec174a 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,73 +1,68 @@ - + - - - - - + Book Library + + - + -
+

Library

Add books to your virtual library

+
- -
-
- - - - - - -
+
@@ -92,5 +87,8 @@

Library

+ + + diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 75ce6c1d..7ef87587 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -16,15 +16,21 @@ function populateStorage() { ); myLibrary.push(book1); myLibrary.push(book2); - render(); } } +const addBtn = document.getElementById("add-btn"); const title = document.getElementById("title"); const author = document.getElementById("author"); const pages = document.getElementById("pages"); const check = document.getElementById("check"); +const bookForm = document.getElementById("book-form"); +// +bookForm.addEventListener("submit", function (e) { + e.preventDefault(); // stop page reload + submit(); // call your function +}); //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() { @@ -37,9 +43,11 @@ 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 book3 = new Book(title.value, author.value, pages.value, check.checked); + myLibrary.push(book3); render(); + bookForm.reset(); + document.getElementById("demo").classList.remove("show"); } } @@ -54,7 +62,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 @@ -76,7 +84,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"; @@ -89,12 +97,12 @@ 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"; delBut.innerHTML = "Delete"; - delBut.addEventListener("clicks", function () { + delBut.addEventListener("click", function () { alert(`You've deleted title: ${myLibrary[i].title}`); myLibrary.splice(i, 1); render(); diff --git a/debugging/book-library/style.css b/debugging/book-library/style.css index 302950cb..42458775 100644 --- a/debugging/book-library/style.css +++ b/debugging/book-library/style.css @@ -1,19 +1,53 @@ +html { + scroll-behavior: smooth; +} + +.header { + background: #2f2e57; + display: flex; + flex-direction: column; + justify-content: center; + align-items: center; +} +.header h1 { + color: white; + font-size: 50px; + padding: 20px; +} +.header p { + color: white; + font-size: 30px; + padding: 20px; +} + .form-group { + background-color: #2f2e57; + color: whitesmoke; width: 400px; - height: 300px; + height: 350px; align-self: left; - padding-left: 20px; + padding: 20px; + margin: 20px auto; + border-radius: 10px; + gap: 10px; } .btn { - display: block; + background-color: skyblue; + display: flex; + margin: 20px auto; + padding: 10px; + border-radius: 10px; + font-size: 20px; + border: none; + outline: none; +} +.btn:focus { + outline: none; + box-shadow: none; } .form-check-label { padding-left: 20px; margin: 5px 0px 5px 0px; } - -button.btn-info { - margin: 20px; -} From b98843c93ec68b8ab59302f170bf555e58ce3bac Mon Sep 17 00:00:00 2001 From: RomanSanaye Date: Thu, 16 Apr 2026 23:10:53 +0100 Subject: [PATCH 2/3] codes have been refactored and meet the general feedback --- debugging/book-library/.vscode/settings.json | 3 + debugging/book-library/index.html | 43 +++--- debugging/book-library/script.js | 137 ++++++++++--------- debugging/book-library/style.css | 39 ++++-- 4 files changed, 128 insertions(+), 94 deletions(-) create mode 100644 debugging/book-library/.vscode/settings.json diff --git a/debugging/book-library/.vscode/settings.json b/debugging/book-library/.vscode/settings.json new file mode 100644 index 00000000..6b665aaa --- /dev/null +++ b/debugging/book-library/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "liveServer.settings.port": 5501 +} diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 97ec174a..d5b1e6cc 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -4,6 +4,7 @@ Book Library + -
+

Library

Add books to your virtual library

+ -
+ +
@@ -36,7 +39,8 @@

Library

name="title" required /> - + + Library name="author" required /> + Library name="pages" required /> -
+ @@ -72,21 +78,18 @@

Library

- + + - - - - - - - +
Author Number of Pages ReadActions
+ + diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 7ef87587..0748840f 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,56 +1,69 @@ let myLibrary = []; -window.addEventListener("load", function (e) { +// =====> Load initial data +window.addEventListener("load", function () { populateStorage(); render(); }); +// =====> Default books function populateStorage() { - if (myLibrary.length == 0) { - let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", 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", + 127, true ); - myLibrary.push(book1); - myLibrary.push(book2); + + myLibrary.push(book1, book2); } } -const addBtn = document.getElementById("add-btn"); +// =====> DOM elements const title = document.getElementById("title"); const author = document.getElementById("author"); const pages = document.getElementById("pages"); const check = document.getElementById("check"); const bookForm = document.getElementById("book-form"); -// +// =====> Form submit bookForm.addEventListener("submit", function (e) { - e.preventDefault(); // stop page reload - submit(); // call your function + e.preventDefault(); + submit(); }); -//check the right input from forms and if its ok -> add the new book (object in array) -//via Book function and start render function + +// =====> Add new book 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(); + + // Validation (improved) + if (!titleValue || !authorValue || !pagesValue) { alert("Please fill all fields!"); - return false; - } else { - let book3 = new Book(title.value, author.value, pages.value, check.checked); - myLibrary.push(book3); - render(); - bookForm.reset(); - document.getElementById("demo").classList.remove("show"); + return; } + + // Create new book + let book = new Book( + titleValue, + authorValue, + Number(pagesValue), + check.checked + ); + + myLibrary.push(book); + + render(); + bookForm.reset(); + + // safely close bootstrap form (optional chaining operator) + document.getElementById("demo")?.classList.remove("show"); } +// =====> Book constructor function Book(title, author, pages, check) { this.title = title; this.author = author; @@ -58,54 +71,52 @@ function Book(title, author, pages, check) { this.check = check; } +// =====> Render table 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); + + // clear old rows (keep header) + for (let i = table.rows.length - 1; i > 0; i--) { + table.deleteRow(i); } - //insert updated row and cells - let length = myLibrary.length; - for (let i = 0; i < length; i++) { + + // render books + myLibrary.forEach((book, index) => { 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; + let readCell = row.insertCell(3); + let actionCell = row.insertCell(4); + + titleCell.textContent = book.title; + authorCell.textContent = book.author; + pagesCell.textContent = book.pages; + + // =====> Read button + let readBtn = document.createElement("button"); + readBtn.className = "btn btn-success"; + readBtn.textContent = book.check ? "Read" : "Unread"; + + readBtn.addEventListener("click", function () { + book.check = !book.check; render(); }); - //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); + readCell.appendChild(readBtn); + + // =====> Delete button + let deleteBtn = document.createElement("button"); + deleteBtn.className = "btn btn-warning"; + deleteBtn.textContent = "Delete"; + + deleteBtn.addEventListener("click", function () { + alert(`Deleted: ${book.title}`); + myLibrary.splice(index, 1); render(); }); - } + + actionCell.appendChild(deleteBtn); + }); } diff --git a/debugging/book-library/style.css b/debugging/book-library/style.css index 42458775..698589ca 100644 --- a/debugging/book-library/style.css +++ b/debugging/book-library/style.css @@ -8,46 +8,63 @@ html { flex-direction: column; justify-content: center; align-items: center; + padding: 20px; } + .header h1 { color: white; font-size: 50px; - padding: 20px; + margin: 10px 0; } + .header p { color: white; font-size: 30px; - padding: 20px; + margin: 10px 0; } .form-group { background-color: #2f2e57; color: whitesmoke; width: 400px; - height: 350px; - align-self: left; - padding: 20px; + height: auto; margin: 20px auto; + padding: 20px; border-radius: 10px; + + display: flex; + flex-direction: column; gap: 10px; } .btn { - background-color: skyblue; - display: flex; + background-color: #4b4a7a; + color: white; margin: 20px auto; - padding: 10px; + padding: 10px 20px; border-radius: 10px; - font-size: 20px; + font-size: 18px; border: none; - outline: none; + cursor: pointer; + transition: 0.2s ease; } + .btn:focus { outline: none; box-shadow: none; } +.btn-read { + background-color: #4b4a7a; + color: white; +} + +.btn-delete { + background-color: #a94442; + color: white; +} + .form-check-label { padding-left: 20px; - margin: 5px 0px 5px 0px; + margin: 5px 0; } From 931a2a3e3db5d4173df88dea56cae935664b2e12 Mon Sep 17 00:00:00 2001 From: RomanSanaye Date: Thu, 16 Apr 2026 23:12:45 +0100 Subject: [PATCH 3/3] remove vscode settings from repo --- debugging/book-library/.vscode/settings.json | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 debugging/book-library/.vscode/settings.json diff --git a/debugging/book-library/.vscode/settings.json b/debugging/book-library/.vscode/settings.json deleted file mode 100644 index 6b665aaa..00000000 --- a/debugging/book-library/.vscode/settings.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "liveServer.settings.port": 5501 -}