From 9198e904ed65fdef7fa0010aacaf711f0f6ec4ce Mon Sep 17 00:00:00 2001 From: UbunMen Date: Fri, 12 Dec 2025 16:06:42 +0000 Subject: [PATCH 1/5] Book Library bugs fixed and Book Library working normal. --- .vscode/launch.json | 15 +++++++ debugging/book-library/index.html | 2 +- debugging/book-library/script.js | 74 ++++++++++++++++++++++++------- 3 files changed, 73 insertions(+), 18 deletions(-) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 00000000..2ba986f6 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,15 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "chrome", + "request": "launch", + "name": "Launch Chrome against localhost", + "url": "http://localhost:8080", + "webRoot": "${workspaceFolder}" + } + ] +} \ No newline at end of file diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 23acfa71..33c05c1e 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -91,6 +91,6 @@

Library

- + diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 75ce6c1d..e4337592 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -7,7 +7,7 @@ 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", @@ -28,21 +28,56 @@ 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() { + // Letters + spaces only (authors) + const lettersOnly = /^[A-Za-z\s]+$/; + + // Title: letters + numbers + spaces (no special characters) + const titleAllowed = /^[A-Za-z0-9\s]+$/; + + // Empty field check 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); - render(); } + + // Title validation + if (!titleAllowed.test(title.value)) { + alert("Title must contain only letters, numbers, and spaces!"); + return false; + } + + // Author validation + if (!lettersOnly.test(author.value)) { + alert("Author name must contain only letters!"); + return false; + } + + // Pages >= 1 + let pagesNum = Number(pages.value); + + if (isNaN(pagesNum) || pagesNum < 1) { + alert("Pages must be a number greater than or equal to 1!"); + return false; + } + + // Create and save the book + let book = new Book(title.value, author.value, pages.value, check.checked); + myLibrary.push(book); + + // Clear inputs (optional) + title.value = ""; + author.value = ""; + pages.value = ""; + check.checked = false; + + render(); } + function Book(title, author, pages, check) { this.title = title; this.author = author; @@ -53,8 +88,8 @@ 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-- { + // delete old table rows (keep header row 0) + for (let n = rowsNumber - 1; n > 0; n--) { table.deleteRow(n); } //insert updated row and cells @@ -72,11 +107,12 @@ function render() { //add and wait for action for read/unread button let changeBut = document.createElement("button"); - changeBut.id = i; + changeBut.id = "read-btn-" + i; changeBut.className = "btn btn-success"; wasReadCell.appendChild(changeBut); let readStatus = ""; - if (myLibrary[i].check == false) { + // show "Yes" when check is true (read), otherwise "No" + if (myLibrary[i].check === true) { readStatus = "Yes"; } else { readStatus = "No"; @@ -89,15 +125,19 @@ function render() { }); //add delete button to every row and render again - let delButton = document.createElement("button"); - delBut.id = i + 5; - deleteCell.appendChild(delBut); + let delBut = document.createElement("button"); + delBut.id = "del-btn-" + i; delBut.className = "btn btn-warning"; delBut.innerHTML = "Delete"; - delBut.addEventListener("clicks", function () { + deleteCell.appendChild(delBut); + + delBut.addEventListener("click", function () { alert(`You've deleted title: ${myLibrary[i].title}`); myLibrary.splice(i, 1); render(); }); } } + + +// Book Library bugs fixed and Book Library working normal. \ No newline at end of file From 7fe03c69c089d83ec8ddb59341c9f969b56c34c5 Mon Sep 17 00:00:00 2001 From: UbunMen Date: Fri, 12 Dec 2025 17:55:05 +0000 Subject: [PATCH 2/5] Bugs fixed --- debugging/book-library/readme.md | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/debugging/book-library/readme.md b/debugging/book-library/readme.md index 3abe8c13..e86a1c29 100644 --- a/debugging/book-library/readme.md +++ b/debugging/book-library/readme.md @@ -12,12 +12,20 @@ My website should be able to: ## Bugs to be fixed -1. Website loads but doesn't show any books -2. Error in console when you try to add a book -3. It uses the title name as the author name -4. Delete button is broken +1. Website loads but doesn't show any books + |Ans: fixed to show books +2. Error in console when you try to add a book + Ans: We can add now. +3. It uses the title name as the author name + Ans: has been fixed +4. Delete button is broken + ANs: has been functional 5. When I add a book that I say I've read - it saves the wrong answer + Ans: it is working normal I think there are other some other small bugs in my code...but I'm lazy so I can't fix them all. + -- Page number used to accept 0 or even negative numbers. Now it only accept >=1 I wish somebody would help me! + +I think I have tried something. From eda7bccc41d80938de78593d72f4ce5e38a91e59 Mon Sep 17 00:00:00 2001 From: UbunMen Date: Sat, 13 Dec 2025 11:31:04 +0000 Subject: [PATCH 3/5] Book Library script updated. --- debugging/book-library/index.html | 94 +++++++++------------------ debugging/book-library/script.js | 103 ++++++++++++------------------ 2 files changed, 71 insertions(+), 126 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 33c05c1e..7704ae09 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -28,69 +28,35 @@

Library

-
- - - - - - - - -
-
+
+ + + + + + + + + + + + +
+ - - - - - - - - - - - - - - - - - - - -
TitleAuthorNumber of PagesRead
+ + + + + + + + + + + +
TitleAuthorNumber of PagesRead
- - - + diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index e4337592..b8911d40 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,19 +1,19 @@ let myLibrary = []; -window.addEventListener("load", function (e) { +window.addEventListener("load", function () { populateStorage(); - render(); }); function populateStorage() { - if (myLibrary.length == 0) { - let book1 = new Book("Robinson 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); render(); @@ -25,50 +25,32 @@ 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() { - // Letters + spaces only (authors) - const lettersOnly = /^[A-Za-z\s]+$/; - - // Title: letters + numbers + spaces (no special characters) - const titleAllowed = /^[A-Za-z0-9\s]+$/; - - // Empty field check - if ( - title.value.trim() === "" || - author.value.trim() === "" || - pages.value.trim() === "" - ) { + // Step 1: store preprocessed raw input in variables + const titleVal = title.value.trim(); + const authorVal = author.value.trim(); + const pagesVal = pages.value.trim(); + const readVal = check.checked; + + // Step 2: validate values + if (!titleVal || !authorVal || !pagesVal) { alert("Please fill all fields!"); - return false; + return; } - // Title validation - if (!titleAllowed.test(title.value)) { - alert("Title must contain only letters, numbers, and spaces!"); - return false; + // Pages validation (must be integer >= 1) + const pagesNum = Number(pagesVal); + if (!Number.isInteger(pagesNum) || pagesNum < 1) { + alert("Pages must be an integer greater than or equal to 1!"); + return; } - // Author validation - if (!lettersOnly.test(author.value)) { - alert("Author name must contain only letters!"); - return false; - } + // Step 3: create Book using validated variables + const book = new Book(titleVal, authorVal, pagesNum, readVal); - // Pages >= 1 - let pagesNum = Number(pages.value); - - if (isNaN(pagesNum) || pagesNum < 1) { - alert("Pages must be a number greater than or equal to 1!"); - return false; - } - - // Create and save the book - let book = new Book(title.value, author.value, pages.value, check.checked); myLibrary.push(book); - // Clear inputs (optional) + // Clear inputs title.value = ""; author.value = ""; pages.value = ""; @@ -77,7 +59,6 @@ function submit() { render(); } - function Book(title, author, pages, check) { this.title = title; this.author = author; @@ -88,56 +69,54 @@ function Book(title, author, pages, check) { function render() { let table = document.getElementById("display"); let rowsNumber = table.rows.length; - // delete old table rows (keep header row 0) + + // Delete old rows (keep header) 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++) { + + // Insert updated rows + 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 + titleCell.textContent = myLibrary[i].title; + authorCell.textContent = myLibrary[i].author; + pagesCell.textContent = myLibrary[i].pages; + + // Read / unread button let changeBut = document.createElement("button"); changeBut.id = "read-btn-" + i; changeBut.className = "btn btn-success"; - wasReadCell.appendChild(changeBut); - let readStatus = ""; - // show "Yes" when check is true (read), otherwise "No" - if (myLibrary[i].check === true) { - readStatus = "Yes"; - } else { - readStatus = "No"; - } - changeBut.innerText = readStatus; + changeBut.textContent = myLibrary[i].check ? "Yes" : "No"; changeBut.addEventListener("click", function () { myLibrary[i].check = !myLibrary[i].check; render(); }); - //add delete button to every row and render again + wasReadCell.appendChild(changeBut); + + // Delete button let delBut = document.createElement("button"); delBut.id = "del-btn-" + i; delBut.className = "btn btn-warning"; - delBut.innerHTML = "Delete"; - deleteCell.appendChild(delBut); + delBut.textContent = "Delete"; delBut.addEventListener("click", function () { alert(`You've deleted title: ${myLibrary[i].title}`); myLibrary.splice(i, 1); render(); }); + + deleteCell.appendChild(delBut); } } -// Book Library bugs fixed and Book Library working normal. \ No newline at end of file +// Book Library script updated. \ No newline at end of file From cb7521d8610e8c71e0b873f90aaad822d2128b20 Mon Sep 17 00:00:00 2001 From: UbunMen Date: Sun, 14 Dec 2025 17:12:13 +0000 Subject: [PATCH 4/5] Modifications regarding input preprocessing table rendering and descriptive names have been made. --- debugging/book-library/script.js | 161 +++++++++++++++---------------- 1 file changed, 77 insertions(+), 84 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index b8911d40..0c5bcda6 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,122 +1,115 @@ -let myLibrary = []; +// script.js (module) -window.addEventListener("load", function () { - populateStorage(); -}); +export {}; -function populateStorage() { - 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(); +// Library array +const myLibrary = []; + +// Book constructor +class Book { + constructor(title, author, pages, read) { + this.title = title; + this.author = author; + this.pages = pages; + this.read = read; } } -const title = document.getElementById("title"); -const author = document.getElementById("author"); -const pages = document.getElementById("pages"); -const check = document.getElementById("check"); +// Default books +const defaultBooks = [ + new Book("Robinson Crusoe", "Daniel Defoe", 252, true), + new Book("The Old Man and the Sea", "Ernest Hemingway", 127, true), +]; + +// DOM references +const titleInput = document.getElementById("title"); +const authorInput = document.getElementById("author"); +const pagesInput = document.getElementById("pages"); +const readCheckbox = document.getElementById("check"); +const submitBtn = document.getElementById("submitBtn"); +const table = document.getElementById("display"); + +// On page load: add default books + +window.addEventListener("DOMContentLoaded", () => { + if (myLibrary.length === 0) { + myLibrary.push(...defaultBooks); + } + render(); +}); + +// Submit listener +submitBtn.addEventListener("click", submit); +// Submit function function submit() { - // Step 1: store preprocessed raw input in variables - const titleVal = title.value.trim(); - const authorVal = author.value.trim(); - const pagesVal = pages.value.trim(); - const readVal = check.checked; + const titleVal = titleInput.value.trim(); + const authorVal = authorInput.value.trim(); + const pagesVal = pagesInput.value.trim(); + const readVal = readCheckbox.checked; - // Step 2: validate values if (!titleVal || !authorVal || !pagesVal) { alert("Please fill all fields!"); return; } - // Pages validation (must be integer >= 1) const pagesNum = Number(pagesVal); if (!Number.isInteger(pagesNum) || pagesNum < 1) { alert("Pages must be an integer greater than or equal to 1!"); return; } - // Step 3: create Book using validated variables - const book = new Book(titleVal, authorVal, pagesNum, readVal); - - myLibrary.push(book); + myLibrary.push(new Book(titleVal, authorVal, pagesNum, readVal)); - // Clear inputs - title.value = ""; - author.value = ""; - pages.value = ""; - check.checked = false; + titleInput.value = ""; + authorInput.value = ""; + pagesInput.value = ""; + readCheckbox.checked = false; render(); } -function Book(title, author, pages, check) { - this.title = title; - this.author = author; - this.pages = pages; - this.check = check; -} - +// Render function function render() { - let table = document.getElementById("display"); - let rowsNumber = table.rows.length; - - // Delete old rows (keep header) - for (let n = rowsNumber - 1; n > 0; n--) { - table.deleteRow(n); - } + const tbody = table.tBodies[0]; - // Insert updated rows - 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); + tbody.innerHTML = ""; - titleCell.textContent = myLibrary[i].title; - authorCell.textContent = myLibrary[i].author; - pagesCell.textContent = myLibrary[i].pages; + myLibrary.forEach((book, index) => { + const row = tbody.insertRow(); - // Read / unread button - let changeBut = document.createElement("button"); - changeBut.id = "read-btn-" + i; - changeBut.className = "btn btn-success"; - changeBut.textContent = myLibrary[i].check ? "Yes" : "No"; + row.insertCell(0).textContent = book.title; + row.insertCell(1).textContent = book.author; + row.insertCell(2).textContent = book.pages; - changeBut.addEventListener("click", function () { - myLibrary[i].check = !myLibrary[i].check; + // Read button + const readCell = row.insertCell(3); + const readBtn = document.createElement("button"); + readBtn.className = "btn btn-success"; + readBtn.textContent = book.read ? "Yes" : "No"; + readBtn.addEventListener("click", () => { + book.read = !book.read; render(); }); - - wasReadCell.appendChild(changeBut); + readCell.appendChild(readBtn); // Delete button - let delBut = document.createElement("button"); - delBut.id = "del-btn-" + i; - delBut.className = "btn btn-warning"; - delBut.textContent = "Delete"; - - delBut.addEventListener("click", function () { - alert(`You've deleted title: ${myLibrary[i].title}`); - myLibrary.splice(i, 1); - render(); + const deleteCell = row.insertCell(4); + const deleteBtn = document.createElement("button"); + deleteBtn.className = "btn btn-warning"; + deleteBtn.textContent = "Delete"; + deleteBtn.addEventListener("click", () => { + if (confirm(`Are you sure you want to delete "${book.title}"?`)) { + myLibrary.splice(index, 1); + render(); + alert("Book deleted successfully!"); + } }); - - deleteCell.appendChild(delBut); - } + deleteCell.appendChild(deleteBtn); + }); } -// Book Library script updated. \ No newline at end of file +// Modifications regarding input preprocessing table rendering and descriptive +// names have been made. \ No newline at end of file From ee1ce9987bc64de99d2eecf8ac7d5a4bd10bd1bb Mon Sep 17 00:00:00 2001 From: UbunMen Date: Sun, 14 Dec 2025 17:13:32 +0000 Subject: [PATCH 5/5] index.hmtl modified to handle ES modules --- debugging/book-library/index.html | 90 +++++++++++++++---------------- 1 file changed, 43 insertions(+), 47 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 7704ae09..b091233c 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,20 +1,16 @@ - + - - + + + Book Library + + + + - - @@ -23,40 +19,40 @@

Library

Add books to your virtual library

- +
-
- - - - - - - - - - - - -
-
- - - - - - - - - - - - -
TitleAuthorNumber of PagesRead
- - +
+ + + + + + + + + + + + +
+ + + + + + + + + + + + + +
TitleAuthorNumber of PagesRead
+ + + +