Skip to content
Closed
71 changes: 47 additions & 24 deletions debugging/book-library/script.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
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 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(book1);
myLibrary.push(book2);
render();
}
}

Expand All @@ -31,14 +30,18 @@ function submit() {
if (
title.value == null ||
title.value == "" ||
author.value == null ||
author.value == "" ||
pages.value == null ||
pages.value == ""
pages.value == "" ||
pages.value <= 0 ||
pages.value != parseInt()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line 38 looks weird. What does that compare?

) {
alert("Please fill all fields!");
alert("Please fill with valid input!");
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);
render();
}
}
Expand All @@ -54,7 +57,7 @@ function render() {
let table = document.getElementById("display");
let rowsNumber = table.rows.length;
//delete old table
for (let n = rowsNumber - 1; n > 0; n-- {
for (let n = rowsNumber - 1; n > 0; n--) {
table.deleteRow(n);
}
//insert updated row and cells
Expand All @@ -71,31 +74,51 @@ function render() {
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 changeButton = document.createElement("button");
changeButton.id = i;
changeButton.className = "btn btn-success";
wasReadCell.appendChild(changeButton);
let readStatus = "";
if (myLibrary[i].check == false) {
readStatus = "Yes";
} else {
readStatus = "No";
} else {
readStatus = "Yes";
}
changeBut.innerText = readStatus;
changeButton.innerText = readStatus;

changeBut.addEventListener("click", function () {
changeButton.addEventListener("click", function () {
myLibrary[i].check = !myLibrary[i].check;
render();
});

// // second option to booleans comparisons
// let changeBut = document.createElement("button");
// changeBut.id = i;
// changeBut.className = "btn btn-success";
// wasReadCell.appendChild(changeBut);
// let readStatus = "";

// // Directly check for boolean value (true or false)
// if (myLibrary[i].check == false) {
// readStatus = "No";
// } else {
// readStatus = "Yes";
// }

// changeBut.innerText = readStatus;

// changeBut.addEventListener("click", function () {
// myLibrary[i].check = !myLibrary[i].check; // Toggle the read status
// render();
// });

//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}`);
const deleteButton = document.createElement("button");
deleteCell.appendChild(deleteButton);
deleteButton.className = "btn btn-warning";
deleteButton.innerHTML = "Delete";
deleteButton.addEventListener("click", function () {
alert(`You are deleting: "${myLibrary[i].title}" Are you sure?!`);
myLibrary.splice(i, 1);
render();
});
Expand Down
Loading