From b3a50484af57a8c1a1b785fe488b1b3806594bcc Mon Sep 17 00:00:00 2001 From: Alireza Seifi Shalamzari <122806598+Alireza1620@users.noreply.github.com> Date: Fri, 17 Jan 2025 12:20:02 +0000 Subject: [PATCH 1/4] Update index.html and add title, and change the oncall from submit() to addBook() Since in script file we have a function called submit() and a variable called the same, it can cause bugs in future --- debugging/book-library/index.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 23acfa71..157f4811 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,7 +1,7 @@ - + Book Libray Library type="submit" value="Submit" class="btn btn-primary" - onclick="submit();" + onclick="addBook();" /> From 2a9e2dd2b220cff343c08c19cffcb4f2cf3c4470 Mon Sep 17 00:00:00 2001 From: Alireza Seifi Shalamzari <122806598+Alireza1620@users.noreply.github.com> Date: Fri, 17 Jan 2025 12:22:09 +0000 Subject: [PATCH 2/4] Update script.js and Fix the logic for PopulateStorage() If the function calles the render() it will cause a bug --- debugging/book-library/script.js | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 75ce6c1d..6fcbbd8f 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -6,17 +6,10 @@ window.addEventListener("load", function (e) { }); 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 - ); - myLibrary.push(book1); - myLibrary.push(book2); - render(); + 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, book2); } } From 7dd5aa95a1da208af69f56a248d1d8571a0e5093 Mon Sep 17 00:00:00 2001 From: Alireza Seifi Shalamzari <122806598+Alireza1620@users.noreply.github.com> Date: Fri, 17 Jan 2025 12:23:03 +0000 Subject: [PATCH 3/4] Update script.js and fix the submit function --- debugging/book-library/script.js | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 6fcbbd8f..c20b4a4a 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -20,20 +20,20 @@ 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 == "" - ) { +function addBook() { + 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); + render(); + // Clear form + title.value = ""; + author.value = ""; + pages.value = ""; + check.checked = false; } function Book(title, author, pages, check) { From b0e1292672b6ffa479dc5314cf791d4fa083c374 Mon Sep 17 00:00:00 2001 From: Alireza Seifi Shalamzari <122806598+Alireza1620@users.noreply.github.com> Date: Fri, 17 Jan 2025 17:37:24 +0000 Subject: [PATCH 4/4] Update script.js and fix the minor bugs --- debugging/book-library/script.js | 63 +++++++++++++------------------- 1 file changed, 25 insertions(+), 38 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index c20b4a4a..5e34cb8e 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -6,7 +6,7 @@ window.addEventListener("load", function (e) { }); function populateStorage() { - if (myLibrary.length === 0) { + 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, book2); @@ -44,53 +44,40 @@ 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; - for (let i = 0; i < length; i++) { - let row = table.insertRow(1); + let table = document.getElementById("display").getElementsByTagName("tbody")[0]; + table.innerHTML = ""; // Clear table + + myLibrary.forEach((book, index) => { + let row = table.insertRow(); 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; + titleCell.textContent = book.title; + authorCell.textContent = book.author; + pagesCell.textContent = book.pages; - changeBut.addEventListener("click", function () { - myLibrary[i].check = !myLibrary[i].check; + // Read/Unread toggle button + let readButton = document.createElement("button"); + readButton.className = "btn btn-success"; + readButton.textContent = book.check ? "Yes" : "No"; + readButton.addEventListener("click", function () { + book.check = !book.check; render(); }); + wasReadCell.appendChild(readButton); - //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); + // Delete button + let deleteButton = document.createElement("button"); + deleteButton.className = "btn btn-warning"; + deleteButton.textContent = "Delete"; + deleteButton.addEventListener("click", function () { + alert(`You've deleted the title: ${book.title}`); + myLibrary.splice(index, 1); render(); }); - } + deleteCell.appendChild(deleteButton); + }); }