diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 23acfa71..806604ac 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,11 +1,9 @@ - - Virtual Library + + @@ -31,7 +29,7 @@

Library

Library /> add the new book (object in array) //via Book function and start render function function submit() { + + // Date : 9/8/2026 + // Purpose : Fix the problem "3. It uses the title name as the author name" + // Change : replace the second title with author.value to fix the bug + let titleValue = title.value.trim(); + let authorValue = author.value.trim(); + let pagesValue = pages.value.trim(); + if ( - title.value == null || - title.value == "" || - pages.value == null || - pages.value == "" + titleValue === "" || + authorValue === "" || + pagesValue === "" ) { - alert("Please fill all fields!"); - return false; - } else { - let book = new Book(title.value, title.value, pages.value, check.checked); - library.push(book); - render(); + displayStatusMessage("Please fill all fields!", true); + return; } + + let book = new Book(titleValue, authorValue, pagesValue, check.checked); + + myLibrary.push(book); + render(); + } function Book(title, author, pages, check) { @@ -52,52 +79,65 @@ 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); + table.innerHTML = ""; + + let thead = document.createElement("thead"); + thead.className = "thead-dark"; + thead.innerHTML = ` + + Title + Author + Number of Pages + Read + + + `; + table.appendChild(thead); + + // Date : 9/8/2026 + // Purpose : Fix the problem "1. Website loads but doesn't show any books" + // Change : Missing ) → script stops executing → nothing renders and add back the closing ) to fix it + 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 () { + + // Date : 9/8/2026 + // Purpose : Fix the problem "5. When I add a book that I say I've read - it saves the wrong answer" + // Change : Correct the logic - if check == true, that means the book was read, so it should show Yes. + changeBut.innerText = myLibrary[i].check ? "Yes" : "No"; + changeBut.addEventListener("click", () => { myLibrary[i].check = !myLibrary[i].check; render(); }); + wasReadCell.appendChild(changeBut); //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}`); + // Date : 9/8/2026 + // Purpose : Fix the problem "4. Delete button is broken" + // Change : Fix the incorrect nanmes "delBut" button and "clicks" event to the correct one "delButton" and "click" respectively + delButton.className = "btn btn-warning"; + delButton.innerText = "Delete"; + delButton.addEventListener("click", () => { + displayStatusMessage(`You've deleted title: ${myLibrary[i].title}`,false); myLibrary.splice(i, 1); render(); }); + deleteCell.appendChild(delButton); } }