From 9f3bdb5b25df562d1e283438733e5f35e3cad126 Mon Sep 17 00:00:00 2001 From: Fares Bakhet Date: Sun, 7 Dec 2025 14:48:10 +0000 Subject: [PATCH 1/4] Finish Book-Library webpage --- debugging/book-library/script.js | 111 ++++++++++++++----------------- 1 file changed, 49 insertions(+), 62 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 75ce6c1d..2b9d3df5 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,22 +1,16 @@ let myLibrary = []; -window.addEventListener("load", function (e) { +window.addEventListener("load", function () { populateStorage(); render(); }); 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 - ); + 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(); } } @@ -25,79 +19,72 @@ 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); - render(); + return; } + + let book = new Book(title.value, author.value, pages.value, check.checked); + myLibrary.push(book); + + // clear form + title.value = ""; + author.value = ""; + pages.value = ""; + check.checked = false; + + render(); } -function Book(title, author, pages, check) { +function Book(title, author, pages, read) { this.title = title; this.author = author; this.pages = pages; - this.check = check; + this.read = read; // renamed from "check" (more readable) } 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); + + // delete old rows + while (table.rows.length > 1) { + table.deleteRow(1); } - //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 - let changeBut = document.createElement("button"); - changeBut.id = i; - changeBut.className = "btn btn-success"; - wasReadCell.appendChild(changeBut); - let readStatus = ""; - if (myLibrary[i].check == false) { - readStatus = "Yes"; - } else { - readStatus = "No"; - } - changeBut.innerText = readStatus; - - changeBut.addEventListener("click", function () { - myLibrary[i].check = !myLibrary[i].check; + + row.insertCell(0).innerHTML = myLibrary[i].title; + row.insertCell(1).innerHTML = myLibrary[i].author; + row.insertCell(2).innerHTML = myLibrary[i].pages; + + // read status toggle button + let readCell = row.insertCell(3); + let readButton = document.createElement("button"); + readButton.className = "btn btn-success"; + readButton.innerText = myLibrary[i].read ? "Yes" : "No"; + + readButton.addEventListener("click", function () { + myLibrary[i].read = !myLibrary[i].read; render(); }); - //add delete button to every row and render again + readCell.appendChild(readButton); + + // delete button + let delCell = row.insertCell(4); 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}`); + delButton.className = "btn btn-warning"; + delButton.innerText = "Delete"; + + delButton.addEventListener("click", function () { + alert(`You've deleted: ${myLibrary[i].title}`); myLibrary.splice(i, 1); render(); }); + + delCell.appendChild(delButton); } } From 0d9263a99587af039186fb72393eb5f46df4d78a Mon Sep 17 00:00:00 2001 From: Fares Bakhet Date: Mon, 8 Dec 2025 13:12:38 +0000 Subject: [PATCH 2/4] Improving he Book Library app --- debugging/book-library/index.html | 183 +++++++++++++++++++----------- debugging/book-library/script.js | 155 ++++++++++++++----------- 2 files changed, 204 insertions(+), 134 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 23acfa71..e36947b9 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,96 +1,147 @@ - + - - - - - + + + Library App + - -

Library

Add books to your virtual library

- -
-
- - - - - - -
- +
- + - + - - - - - - - - - +
Title AuthorNumber of PagesPages ReadAction
- + + + + + diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 2b9d3df5..bacb9f50 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,90 +1,109 @@ -let myLibrary = []; - -window.addEventListener("load", function () { - 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); +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"); -function submit() { - if (title.value === "" || author.value === "" || pages.value === "") { - alert("Please fill all fields!"); +const library = [ + new Book("Robinson Crusoe", "Daniel Defoe", 252, true), + new Book("The Old Man and the Sea", "Ernest Hemingway", 127, true), +]; + + +const bookForm = document.getElementById("bookForm"); +const titleInput = document.getElementById("titleInput"); +const authorInput = document.getElementById("authorInput"); +const pagesInput = document.getElementById("pagesInput"); +const readCheckbox = document.getElementById("readCheckbox"); +const tableBody = document.querySelector("#displayTable tbody"); + + +bookForm.addEventListener("submit", (event) => { + event.preventDefault(); + + const title = titleInput.value.trim(); + const author = authorInput.value.trim(); + const pages = Number(pagesInput.value); + const read = readCheckbox.checked; + + + if (!title || !author || !Number.isInteger(pages) || pages <= 0) { + alert("Please enter valid book details."); return; } - let book = new Book(title.value, author.value, pages.value, check.checked); - myLibrary.push(book); - // clear form - title.value = ""; - author.value = ""; - pages.value = ""; - check.checked = false; + library.push(new Book(title, author, pages, read)); + + + bookForm.reset(); + render(); -} +}); -function Book(title, author, pages, read) { - this.title = title; - this.author = author; - this.pages = pages; - this.read = read; // renamed from "check" (more readable) -} function render() { - let table = document.getElementById("display"); - - // delete old rows - while (table.rows.length > 1) { - table.deleteRow(1); - } - // insert updated rows - for (let i = 0; i < myLibrary.length; i++) { - let row = table.insertRow(1); + tableBody.innerHTML = ""; + + library.forEach((book, index) => { + const row = document.createElement("tr"); + + row.innerHTML = ` + ${book.title} + ${book.author} + ${book.pages} + + + + + + + `; + + tableBody.appendChild(row); + }); +} - row.insertCell(0).innerHTML = myLibrary[i].title; - row.insertCell(1).innerHTML = myLibrary[i].author; - row.insertCell(2).innerHTML = myLibrary[i].pages; - // read status toggle button - let readCell = row.insertCell(3); - let readButton = document.createElement("button"); - readButton.className = "btn btn-success"; - readButton.innerText = myLibrary[i].read ? "Yes" : "No"; +tableBody.addEventListener("click", (event) => { + const button = event.target.closest("button"); + if (!button) return; - readButton.addEventListener("click", function () { - myLibrary[i].read = !myLibrary[i].read; - render(); - }); + const index = Number(button.dataset.index); + const action = button.dataset.action; - readCell.appendChild(readButton); - // delete button - let delCell = row.insertCell(4); - let delButton = document.createElement("button"); - delButton.className = "btn btn-warning"; - delButton.innerText = "Delete"; + if (action === "toggle") { + library[index].read = !library[index].read; + render(); + } - delButton.addEventListener("click", function () { - alert(`You've deleted: ${myLibrary[i].title}`); - myLibrary.splice(i, 1); - render(); - }); - delCell.appendChild(delButton); + if (action === "delete") { + const removedTitle = library[index].title; + library.splice(index, 1); + render(); + alert(`Deleted: ${removedTitle}`); } -} +}); + + +render(); From dbc998a0d142e8aaf464909111899a85c2841dd6 Mon Sep 17 00:00:00 2001 From: Fares Bakhet Date: Tue, 16 Dec 2025 21:04:20 +0000 Subject: [PATCH 3/4] Fix HTML file --- debugging/book-library/index.html | 198 ++++++++---------------------- 1 file changed, 54 insertions(+), 144 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index e36947b9..61a8f3a0 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,147 +1,57 @@ - - - - Library App - - - - -
-

Library

-

Add books to your virtual library

-
- - - -
-
- - - - - - - - - - - - -
-
- - - - - - - - - - - - -
TitleAuthorPagesReadAction
- - - - - - - + + + + Library App + + + +
+

Library

+

Add books to your virtual library

+
+ + + +
+
+ + + + + + + + + + + + +
+
+ + + + + + + + + + + + +
TitleAuthorPagesReadAction
+ + + + + + + + + From 615130e1c3f8052ed02121ab82a2b8faf4555ddc Mon Sep 17 00:00:00 2001 From: Fares Bakhet Date: Wed, 17 Dec 2025 10:06:13 +0000 Subject: [PATCH 4/4] Fix script.js file --- debugging/book-library/script.js | 74 ++++++++++++++++---------------- 1 file changed, 37 insertions(+), 37 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index bacb9f50..923bbb06 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -7,13 +7,11 @@ class Book { } } - const library = [ new Book("Robinson Crusoe", "Daniel Defoe", 252, true), new Book("The Old Man and the Sea", "Ernest Hemingway", 127, true), ]; - const bookForm = document.getElementById("bookForm"); const titleInput = document.getElementById("titleInput"); const authorInput = document.getElementById("authorInput"); @@ -21,7 +19,6 @@ const pagesInput = document.getElementById("pagesInput"); const readCheckbox = document.getElementById("readCheckbox"); const tableBody = document.querySelector("#displayTable tbody"); - bookForm.addEventListener("submit", (event) => { event.preventDefault(); @@ -30,59 +27,65 @@ bookForm.addEventListener("submit", (event) => { const pages = Number(pagesInput.value); const read = readCheckbox.checked; - if (!title || !author || !Number.isInteger(pages) || pages <= 0) { alert("Please enter valid book details."); return; } - library.push(new Book(title, author, pages, read)); - - bookForm.reset(); - - render(); }); - function render() { - tableBody.innerHTML = ""; library.forEach((book, index) => { const row = document.createElement("tr"); - row.innerHTML = ` - ${book.title} - ${book.author} - ${book.pages} - - - - - - + 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"); + readCell.innerHTML = ` + + `; + + const actionCell = document.createElement("td"); + actionCell.innerHTML = ` + `; + row.append( + titleCell, + authorCell, + pagesCell, + readCell, + actionCell + ); + tableBody.appendChild(row); }); } - tableBody.addEventListener("click", (event) => { const button = event.target.closest("button"); if (!button) return; @@ -90,13 +93,11 @@ tableBody.addEventListener("click", (event) => { const index = Number(button.dataset.index); const action = button.dataset.action; - if (action === "toggle") { library[index].read = !library[index].read; render(); } - if (action === "delete") { const removedTitle = library[index].title; library.splice(index, 1); @@ -105,5 +106,4 @@ tableBody.addEventListener("click", (event) => { } }); - render();