From c5275cf69b0320c6eb5405ac2923c7b505da9455 Mon Sep 17 00:00:00 2001 From: AdiyahFarhan Date: Tue, 5 Aug 2025 21:18:25 +0100 Subject: [PATCH 1/3] fixes the bugs on the website, Now the list of books can be viewed, add a book and removes a book successfully --- debugging/book-library/script.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 75ce6c1d..7870f55a 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -37,8 +37,8 @@ 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 book = new Book(title.value, author.value, pages.value, check.checked); + myLibrary.push(book); render(); } } @@ -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 @@ -77,9 +77,9 @@ function render() { wasReadCell.appendChild(changeBut); let readStatus = ""; if (myLibrary[i].check == false) { - readStatus = "Yes"; - } else { readStatus = "No"; + } else { + readStatus = "Yes"; } changeBut.innerText = readStatus; @@ -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 6992fadeabe6e4256bdc38d9215ed584372f8c69 Mon Sep 17 00:00:00 2001 From: AdiyahFarhan Date: Wed, 13 Aug 2025 20:39:41 +0100 Subject: [PATCH 2/3] Remove the validation errors. The HTML validated using https://validator.w3.org/ --- debugging/book-library/index.html | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 23acfa71..da2a0cc7 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,12 +1,9 @@ - - + Book Library Project | (Adiyah Farhan) + + @@ -31,7 +28,7 @@

Library

Library /> Date: Wed, 13 Aug 2025 20:41:00 +0100 Subject: [PATCH 3/3] Update the JavaScript according to the "feedback to improve the code" --- debugging/book-library/script.js | 111 +++++++++++++++++++------------ 1 file changed, 70 insertions(+), 41 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 7870f55a..4e7d2a3c 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -7,97 +7,126 @@ 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("Robison 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(); } } -const title = document.getElementById("title"); -const author = document.getElementById("author"); -const pages = document.getElementById("pages"); -const check = document.getElementById("check"); +const titleElement = document.getElementById("title"); +const authorElement = document.getElementById("author"); +const pagesElement = document.getElementById("pages"); +const checkElement = 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 == "" - ) { + if (!titleElement.value || !authorElement.value || !pagesElement.value) { alert("Please fill all fields!"); return false; + } else if (!titleElement.value.match(/^(?=.*[a-zA-Z])[a-zA-Z ]+$/)) { + alert("Please enter a valid book title!"); + return false; + } else if (!authorElement.value.match(/^(?=.*[a-zA-Z])[a-zA-Z ]+$/)) { + alert("Please enter a valid author name!"); + return false; + } else if (!pagesElement.value.match(/^[0-9]+$/)) { + alert("Please enter a valid number of pages!"); + return false; } else { - let book = new Book(title.value, author.value, pages.value, check.checked); + let book = new Book( + titleElement.value, + authorElement.value, + pagesElement.value, + checkElement.checked + ); + myLibrary.push(book); render(); } } function Book(title, author, pages, check) { - this.title = title; - this.author = author; - this.pages = pages; + this.title = title.trim(); //remove leading/trailing spaces + this.author = author.trim(); + this.pages = Number(pages); // Ensure pages is a number this.check = check; } function render() { - let table = document.getElementById("display"); - let rowsNumber = table.rows.length; + //Method 1: To clear the table + //let table = document.getElementById("display"); + //delete all rows except the first one (header) + //let rowsNumber = table.rows.length; //delete old table - for (let n = rowsNumber - 1; n > 0; n--) { - table.deleteRow(n); - } + //for (let n = rowsNumber - 1; n > 0; n--) { + //table.deleteRow(n); + //} + + //Method 2: To clear the table in one operation + //replaceChildren() is a modern method to clear all children of an element + let tableBody = document.querySelector("tbody"); + tableBody.replaceChildren(); // Clear the table body + //insert updated row and cells let length = myLibrary.length; for (let i = 0; i < length; i++) { - let row = table.insertRow(1); + let row = tableBody.insertRow(0); // Insert a new row at the beginning of the table body 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; + + //innerHTML was replaced with textContent for cleaner text handling / code semantics. + titleCell.textContent = myLibrary[i].title; + authorCell.textContent = myLibrary[i].author; + pagesCell.textContent = 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 readButton = document.createElement("button"); + + // readButton.id = i; / + // No need for an ID here, as we can use the index directly + readButton.className = "btn btn-success"; + wasReadCell.appendChild(readButton); let readStatus = ""; if (myLibrary[i].check == false) { readStatus = "No"; } else { readStatus = "Yes"; } - changeBut.innerText = readStatus; + readButton.innerText = readStatus; - changeBut.addEventListener("click", function () { + readButton.addEventListener("click", function () { myLibrary[i].check = !myLibrary[i].check; 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); - render(); + let deleteButton = document.createElement("button"); + + // No need to set an ID for the delete button, as we can use the index directly + //deleteButton.id = i + 5; + deleteCell.appendChild(deleteButton); + deleteButton.className = "btn btn-warning"; + deleteButton.textContent = "Delete"; + deleteButton.addEventListener("click", function () { + const deletedTitle = myLibrary[i].title; //save the title of the book to be deleted + myLibrary.splice(i, 1); // Remove the book from the library array + render(); // update the table after deletion + + // Use setTimeout to ensure the alert is shown after the render + // This is useful to ensure the UI is updated before showing the alert + setTimeout(() => { + alert(`You've deleted title: ${deletedTitle}`); + }, 0.5); }); } }