From da9328ef81f06e5559fd2e35407e15cda79c5337 Mon Sep 17 00:00:00 2001 From: Alina Sofragiu Date: Mon, 3 Aug 2026 19:00:51 +0100 Subject: [PATCH 1/2] Refactor book submission and rendering logic for improved clarity and functionality --- debugging/book-library/script.js | 55 ++++++++++++++++++-------------- 1 file changed, 31 insertions(+), 24 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 75ce6c1d..312b8718 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,6 +1,6 @@ let myLibrary = []; -window.addEventListener("load", function (e) { +window.addEventListener("load", function () { populateStorage(); render(); }); @@ -16,7 +16,6 @@ function populateStorage() { ); myLibrary.push(book1); myLibrary.push(book2); - render(); } } @@ -25,20 +24,20 @@ 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 == "" || - pages.value == null || - pages.value == "" - ) { + if (title.value === "" || author.value === "" || 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); + + title.value = ""; + author.value = ""; + pages.value = ""; + check.checked = false; + render(); } } @@ -53,12 +52,15 @@ 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 + for (let n = rowsNumber - 1; n > 0; n--) { table.deleteRow(n); } - //insert updated row and cells + + // insert updated rows let length = myLibrary.length; + for (let i = 0; i < length; i++) { let row = table.insertRow(1); let titleCell = row.insertCell(0); @@ -66,35 +68,40 @@ 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; - //add and wait for action for read/unread button + // Read button let changeBut = document.createElement("button"); changeBut.id = i; 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"; } + changeBut.innerText = readStatus; + wasReadCell.appendChild(changeBut); changeBut.addEventListener("click", function () { myLibrary[i].check = !myLibrary[i].check; render(); }); - //add delete button to every row and render again + // Delete button 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; + delButton.className = "btn btn-warning"; + delButton.innerHTML = "Delete"; + + deleteCell.appendChild(delButton); + + delButton.addEventListener("click", function () { alert(`You've deleted title: ${myLibrary[i].title}`); myLibrary.splice(i, 1); render(); From c28263f518fedd8a78ae7cf00d8ae919530054ae Mon Sep 17 00:00:00 2001 From: Alina Sofragiu Date: Wed, 5 Aug 2026 21:02:44 +0100 Subject: [PATCH 2/2] Enhance HTML structure and improve JavaScript functionality for book library --- debugging/book-library/index.html | 79 ++++++--------- debugging/book-library/script.js | 153 ++++++++++++++---------------- debugging/book-library/style.css | 4 +- 3 files changed, 101 insertions(+), 135 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 23acfa71..05d8c33a 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,20 +1,20 @@ - + - - + + + My Book Library + + - + + @@ -23,49 +23,31 @@

Library

Add books to your virtual library

-
- - - - - - + + + + + + + + + +
@@ -75,22 +57,15 @@

Library

Title Author - Number of Pages + Pages Read - + Delete - - - - - - - - - + + - + diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 312b8718..65d857da 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,110 +1,103 @@ -let myLibrary = []; +const myLibrary = []; -window.addEventListener("load", function () { +window.addEventListener("load", () => { populateStorage(); render(); }); +function Book(title, author, pages, check) { + this.title = title; + this.author = author; + this.pages = pages; + this.check = check; +} + function populateStorage() { - if (myLibrary.length == 0) { - let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true); - let book2 = new Book( - "The Old Man and the Sea", - "Ernest Hemingway", - "127", - true + if (myLibrary.length === 0) { + myLibrary.push( + new Book("Robinson Crusoe", "Daniel Defoe", 252, true), + new Book("The Old Man and the Sea", "Ernest Hemingway", 127, true) ); - myLibrary.push(book1); - myLibrary.push(book2); } } -const title = document.getElementById("title"); -const author = document.getElementById("author"); -const pages = document.getElementById("pages"); -const check = document.getElementById("check"); +const titleInput = document.getElementById("title"); +const authorInput = document.getElementById("author"); +const pagesInput = document.getElementById("pages"); +const checkInput = document.getElementById("check"); function submit() { - if (title.value === "" || author.value === "" || pages.value === "") { + const title = titleInput.value.trim(); + const author = authorInput.value.trim(); + const pages = Number(pagesInput.value); + + if (title === "" || author === "" || !pages) { alert("Please fill all fields!"); - return false; - } else { - let book = new Book(title.value, author.value, pages.value, check.checked); + return; + } - myLibrary.push(book); + myLibrary.push(new Book(title, author, pages, checkInput.checked)); - title.value = ""; - author.value = ""; - pages.value = ""; - check.checked = false; + titleInput.value = ""; + authorInput.value = ""; + pagesInput.value = ""; + checkInput.checked = false; - render(); - } + render(); } -function Book(title, author, pages, check) { - this.title = title; - this.author = author; - this.pages = pages; - this.check = check; -} +window.submit = submit; function render() { - let table = document.getElementById("display"); - let rowsNumber = table.rows.length; + const tbody = document.querySelector("#display tbody"); - // delete old table rows - for (let n = rowsNumber - 1; n > 0; n--) { - table.deleteRow(n); - } + tbody.textContent = ""; + + myLibrary.forEach((book, index) => { + const row = document.createElement("tr"); + + const titleCell = document.createElement("td"); + titleCell.textContent = book.title; + + const authorCell = document.createElement("td"); + authorCell.textContent = book.author; + + const pagesCell = document.createElement("td"); + pagesCell.textContent = book.pages; + + const readCell = document.createElement("td"); + + const readButton = document.createElement("button"); + readButton.className = "btn btn-success"; + readButton.textContent = book.check ? "Yes" : "No"; - // insert updated rows - let 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; - - // Read button - let changeBut = document.createElement("button"); - changeBut.id = i; - changeBut.className = "btn btn-success"; - - let readStatus = ""; - if (myLibrary[i].check === true) { - readStatus = "Yes"; - } else { - readStatus = "No"; - } - - changeBut.innerText = readStatus; - wasReadCell.appendChild(changeBut); - - changeBut.addEventListener("click", function () { - myLibrary[i].check = !myLibrary[i].check; + readButton.addEventListener("click", () => { + book.check = !book.check; render(); }); - // Delete button - let delButton = document.createElement("button"); - delButton.id = i + 5; - delButton.className = "btn btn-warning"; - delButton.innerHTML = "Delete"; + readCell.appendChild(readButton); - deleteCell.appendChild(delButton); + const deleteCell = document.createElement("td"); + + const deleteButton = document.createElement("button"); + deleteButton.className = "btn btn-warning"; + deleteButton.textContent = "Delete"; + + deleteButton.addEventListener("click", () => { + const deletedTitle = book.title; + + myLibrary.splice(index, 1); - delButton.addEventListener("click", function () { - alert(`You've deleted title: ${myLibrary[i].title}`); - myLibrary.splice(i, 1); render(); + + alert(`You've deleted "${deletedTitle}".`); }); - } + + deleteCell.appendChild(deleteButton); + + row.append(titleCell, authorCell, pagesCell, readCell, deleteCell); + + tbody.appendChild(row); + }); } diff --git a/debugging/book-library/style.css b/debugging/book-library/style.css index 302950cb..f4514475 100644 --- a/debugging/book-library/style.css +++ b/debugging/book-library/style.css @@ -1,7 +1,5 @@ .form-group { width: 400px; - height: 300px; - align-self: left; padding-left: 20px; } @@ -11,7 +9,7 @@ .form-check-label { padding-left: 20px; - margin: 5px 0px 5px 0px; + margin: 5px 0; } button.btn-info {