From 820bf57822534bfeaf22409ab99741e4eb38ddfb Mon Sep 17 00:00:00 2001 From: Francesco Romano Monda Date: Fri, 14 Aug 2026 19:24:24 +0100 Subject: [PATCH 1/3] Fix book list rendering, validation, and bugs --- debugging/book-library/index.html | 18 +++++------------- debugging/book-library/script.js | 31 +++++++++++++++++-------------- 2 files changed, 22 insertions(+), 27 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 23acfa71e..2e4806e56 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,4 +1,4 @@ - + @@ -31,7 +31,7 @@

Library

Library /> Library type="submit" value="Submit" class="btn btn-primary" - onclick="submit();" + onclick="submit()" />
@@ -80,15 +80,7 @@

Library

- - - - - - - - - + diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 75ce6c1d3..b2836c201 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", @@ -29,16 +29,19 @@ const check = document.getElementById("check"); //via Book function and start render function function submit() { if ( - title.value == null || - title.value == "" || - pages.value == null || - pages.value == "" + title.value === null || + title.value === "" || + author.value === null || + author.value === "" || + pages.value === null || + pages.value === "" ) { 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 +57,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 +79,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"; @@ -90,11 +93,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 473ba31d63dd435c26a42701fb1fa2b709c4d2f2 Mon Sep 17 00:00:00 2001 From: fromonda Date: Sat, 15 Aug 2026 16:34:49 +0100 Subject: [PATCH 2/3] Improve HTML, CSS, and JavaScript --- debugging/book-library/index.html | 40 +++---- debugging/book-library/script.js | 175 ++++++++++++++++-------------- debugging/book-library/style.css | 6 +- 3 files changed, 112 insertions(+), 109 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 2e4806e56..58dbd2ea0 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,12 +1,9 @@ - - + My Library + + @@ -36,6 +33,7 @@

Library

id="title" name="title" required + minlength="1" /> Library id="author" name="author" required + minlength="1" /> Library id="pages" name="pages" required + min="1" + step="1" /> - - +
+ + +
+ + - @@ -80,9 +74,9 @@

Library

- +
- + diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index b2836c201..555788833 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,51 +1,49 @@ -let myLibrary = []; +const myLibrary = []; -window.addEventListener("load", function (e) { - populateStorage(); - render(); -}); - -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(); - } -} +// Get elements from HTML +const titleInput = document.getElementById("title"); +const authorInput = document.getElementById("author"); +const pagesInput = document.getElementById("pages"); +const checkInput = document.getElementById("check"); +const submitBtn = document.getElementById("submitBtn"); +const bookList = document.getElementById("bookList"); +const messageEl = document.getElementById("message"); + +// Listen for Submit button click +submitBtn.addEventListener("click", submitBook); + +// Add initial books +const book1 = new Book("Robinson Crusoe", "Daniel Defoe", 252, true); +const book2 = new Book( + "The Old Man and the Sea", + "Ernest Hemingway", + 127, + true +); + +myLibrary.push(book1); +myLibrary.push(book2); +render(); + +// Check the right input from forms and if it's okay, +// add the new book object to the array and render +function submitBook() { + const title = titleInput.value.trim(); + const author = authorInput.value.trim(); + const pages = Number(pagesInput.value); -const title = document.getElementById("title"); -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 === "" || - author.value === null || - author.value === "" || - pages.value === null || - pages.value === "" - ) { - alert("Please fill all fields!"); - return false; - } else { - let book = new Book(title.value, author.value, pages.value, check.checked); - - myLibrary.push(book); - render(); + if (!title || !author || !Number.isInteger(pages) || pages < 1) { + showMessage("Please enter valid book information.", "danger"); + return; } + + const book = new Book(title, author, pages, checkInput.checked); + + myLibrary.push(book); + render(); } +// Book constructor function Book(title, author, pages, check) { this.title = title; this.author = author; @@ -54,53 +52,64 @@ 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); - } - //insert updated row and cells - let length = myLibrary.length; + // Delete old table rows + bookList.replaceChildren(); + + // Insert updated rows and cells + const length = myLibrary.length; + for (let i = 0; i < 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 === true) { - readStatus = "Yes"; - } else { - readStatus = "No"; - } - changeBut.innerText = readStatus; - - changeBut.addEventListener("click", function () { + const row = bookList.insertRow(); + + const titleCell = row.insertCell(0); + const authorCell = row.insertCell(1); + const pagesCell = row.insertCell(2); + const wasReadCell = row.insertCell(3); + const deleteCell = row.insertCell(4); + + titleCell.textContent = myLibrary[i].title; + authorCell.textContent = myLibrary[i].author; + pagesCell.textContent = myLibrary[i].pages; + + // Add and wait for action for read/unread button + const changeButton = document.createElement("button"); + changeButton.className = "btn btn-success"; + + const readStatus = myLibrary[i].check ? "Yes" : "No"; + changeButton.textContent = readStatus; + + wasReadCell.appendChild(changeButton); + + changeButton.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}`); + // Add delete button to every row + const deleteButton = document.createElement("button"); + deleteButton.className = "btn btn-warning"; + deleteButton.textContent = "Delete"; + + deleteCell.appendChild(deleteButton); + + deleteButton.addEventListener("click", function () { + const deletedTitle = myLibrary[i].title; + myLibrary.splice(i, 1); render(); + + showMessage(`You've deleted title: ${deletedTitle}`, "success"); }); } } + +// Display a message without using alert() +function showMessage(message, type) { + messageEl.textContent = message; + messageEl.className = `alert alert-${type}`; + messageEl.hidden = false; + + setTimeout(function () { + messageEl.hidden = true; + }, 3000); +} diff --git a/debugging/book-library/style.css b/debugging/book-library/style.css index 302950cb8..44b114fa8 100644 --- a/debugging/book-library/style.css +++ b/debugging/book-library/style.css @@ -1,7 +1,7 @@ .form-group { width: 400px; height: 300px; - align-self: left; + align-self: flex-start; padding-left: 20px; } @@ -10,8 +10,8 @@ } .form-check-label { - padding-left: 20px; - margin: 5px 0px 5px 0px; + padding-left: 5px; + margin: 0px 0px 10px 0px; } button.btn-info { From f4f6712557ce2de87d177fb533184edebfd37077 Mon Sep 17 00:00:00 2001 From: Francesco Romano Monda Date: Sat, 15 Aug 2026 22:42:43 +0100 Subject: [PATCH 3/3] Improve initialization and add error message --- debugging/book-library/index.html | 2 + debugging/book-library/script.js | 61 +++++++++++++++++++------------ 2 files changed, 39 insertions(+), 24 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 58dbd2ea0..9e6b553b5 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -24,6 +24,8 @@

Library

Add new book + +
diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 555788833..d1c6bb2ce 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,29 +1,42 @@ const myLibrary = []; -// Get elements from HTML -const titleInput = document.getElementById("title"); -const authorInput = document.getElementById("author"); -const pagesInput = document.getElementById("pages"); -const checkInput = document.getElementById("check"); -const submitBtn = document.getElementById("submitBtn"); -const bookList = document.getElementById("bookList"); -const messageEl = document.getElementById("message"); - -// Listen for Submit button click -submitBtn.addEventListener("click", submitBook); - -// Add initial books -const book1 = new Book("Robinson Crusoe", "Daniel Defoe", 252, true); -const book2 = new Book( - "The Old Man and the Sea", - "Ernest Hemingway", - 127, - true -); - -myLibrary.push(book1); -myLibrary.push(book2); -render(); +let titleInput; +let authorInput; +let pagesInput; +let checkInput; +let submitBtn; +let bookList; +let messageEl; + +function init() { + // Get elements from HTML + titleInput = document.getElementById("title"); + authorInput = document.getElementById("author"); + pagesInput = document.getElementById("pages"); + checkInput = document.getElementById("check"); + submitBtn = document.getElementById("submitBtn"); + bookList = document.getElementById("bookList"); + messageEl = document.getElementById("message"); + + // Listen for Submit button click + submitBtn.addEventListener("click", submitBook); + + // Add initial books + const book1 = new Book("Robinson Crusoe", "Daniel Defoe", 252, true); + const book2 = new Book( + "The Old Man and the Sea", + "Ernest Hemingway", + 127, + true + ); + + myLibrary.push(book1); + myLibrary.push(book2); + + render(); +} + +init(); // Check the right input from forms and if it's okay, // add the new book object to the array and render