From 9c74db33ee98a8d5ce7a576f4a368b1b98937b74 Mon Sep 17 00:00:00 2001 From: delmorallopez <124817272+delmorallopez@users.noreply.github.com> Date: Wed, 13 Aug 2025 10:25:36 +0100 Subject: [PATCH 1/3] Revision --- debugging/book-library/script.js | 76 ++++++++++++++++---------------- 1 file changed, 38 insertions(+), 38 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 75ce6c1d..ee1613f5 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,3 +1,5 @@ + + let myLibrary = []; window.addEventListener("load", function (e) { @@ -7,16 +9,15 @@ 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(); } } @@ -28,17 +29,19 @@ 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 == "" - ) { - alert("Please fill all fields!"); + const cleanTitle = title.value.trim(); + const cleanAuthor = author.value.trim(); + const cleanPages = parseInt(pages.value, 10); + + if (!cleanTitle|| !cleanAuthor) { // title and author doesn't allowed to contain only space characters + alert("Title and Author cannot be empty or spaces only."); + return false; + } else if (isNaN(cleanPages) || cleanPages <= 0) { // the value of type of pages must be a integer + alert("Pages must be a positive whole number."); return false; } else { - let book = new Book(title.value, title.value, pages.value, check.checked); - library.push(book); + let book = new Book(cleanTitle, cleanAuthor, pages.value, check.checked); //// title and author be allowed to contain leading or trailing space characters, we storage using parseInt + myLibrary.push(book); render(); } } @@ -52,11 +55,10 @@ 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); - } + // Keep only the header row + table.innerHTML = table.rows[0].outerHTML; + //insert updated row and cells let length = myLibrary.length; for (let i = 0; i < length; i++) { @@ -66,37 +68,35 @@ function render() { 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; + 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 toggleReadButton = document.createElement("button"); + toggleReadButton.className = "btn btn-success"; + wasReadCell.appendChild(toggleReadButton); let readStatus = ""; - if (myLibrary[i].check == false) { - readStatus = "Yes"; - } else { - readStatus = "No"; - } - changeBut.innerText = readStatus; - changeBut.addEventListener("click", function () { + readStatus = myLibrary[i].check ? "Yes" : "No"; + + toggleReadButton.innerText = readStatus; + + toggleReadButton.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}`); - myLibrary.splice(i, 1); + let deleteBookButton = document.createElement("button"); + deleteCell.appendChild(deleteBookButton); + deleteBookButton.className = "btn btn-warning"; + deleteBookButton.innerHTML = "Delete"; + deleteBookButton.addEventListener("click", function () { + const deleteTitle = myLibrary[i].title; //store first + myLibrary.splice(i, 1); // delete it + alert(`You have deleted title: ${deleteTitle}`); render(); }); } From 095567b552e0297321771156ab513deb42e7b7f2 Mon Sep 17 00:00:00 2001 From: delmorallopez <124817272+delmorallopez@users.noreply.github.com> Date: Thu, 14 Aug 2025 11:32:39 +0100 Subject: [PATCH 2/3] Review w3.org --- debugging/book-library/index.html | 18 ++++++------------ debugging/book-library/script.js | 16 +++++++++++----- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 23acfa71..7cc83b96 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,12 +1,10 @@
-