Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 47 additions & 39 deletions debugging/book-library/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@ 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();
let book2 = new Book("The Old Man and the Sea","Ernest Hemingway",127,true);

// Changed by Chun Yan Wong
// Date : 9/8/2026
// Purpose : Fix the problem "2. Error in console when you try to add a book"
// Change : replace Librarty with the correct variable myLibrary
myLibrary.push(book1, book2);
}
}

Expand All @@ -29,17 +27,22 @@ 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.trim() === "" ||
pages.value.trim() === "" ||
author.value.trim() === ""
) {
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;
}


// Changed by Chun Yan Wong
// 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 book = new Book(title.value, author.value, pages.value, check.checked);
mylibrary.push(book);
render();
}
}

Expand All @@ -54,50 +57,55 @@ 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);
while (table.rows.length > 1) {
table.deleteRow(1);
}
//insert updated row and cells
let length = myLibrary.length;
for (let i = 0; i < length; i++) {
let row = table.insertRow(1);

// Changed by Chun Yan Wong
// 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 () {
// Changed by Chun Yan Wong
// 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 () {
// Changed by Chun Yan Wong
// 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", () => {
alert(`You've deleted title: ${myLibrary[i].title}`);
myLibrary.splice(i, 1);
render();
});
deleteCell.appendChild(delButton);
}
}
Loading