Skip to content
Closed
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
106 changes: 59 additions & 47 deletions debugging/book-library/script.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
let myLibrary = [];
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can we declare myLibrary in a way that prevents it from being accidentally reassigned?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can you address this comment?


window.addEventListener("load", function (e) {
window.addEventListener("load", function () {
populateStorage();
render();
});
Expand Down Expand Up @@ -28,76 +28,88 @@ 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 == ""
) {
alert("Please fill all fields!");
// 1. Preprocess and store cleaned values in variables / removes spaces from the beginning and end
const cleanTitle = title.value.trim();
const cleanAuthor = author.value.trim();

// Convert string input to a Number for calculation/validation
const pageCount = Number(pages.value);
const isRead = check.checked;

// 2. VALIDATION: // Check if strings are empty after trimming spaces
if (cleanTitle === "" || cleanAuthor === "") {
alert("Title and Author cannot be empty!");
return false;
}
// Check if page count is a valid number and greater than 0
if (isNaN(pageCount) || pageCount <= 0) {
alert("Please enter a valid number of pages!");
return false;
} else {
let book = new Book(title.value, title.value, pages.value, check.checked);
library.push(book);
render();
}

// 3. If all validations pass, create a new Book object and add it to the library
let book = new Book(cleanTitle, cleanAuthor, pageCount, isRead);
myLibrary.push(book);

// 4. Clear the form fields after submission
title.value = "";
author.value = "";
pages.value = "";
check.checked = false;

render();
}

function Book(title, author, pages, check) {
this.title = title;
this.author = author;
this.pages = pages;
this.check = check;
class Book {
constructor(title, author, pages, check) {
this.title = title;
this.author = author;
this.pages = pages;
this.check = check;
}
}

function render() {
let table = document.getElementById("display");
let rowsNumber = table.rows.length;
const table = document.getElementById("display");
const tbody = table.querySelector("tbody");
//delete old table
for (let n = rowsNumber - 1; n > 0; n-- {
table.deleteRow(n);
if (tbody) {
tbody.innerHTML = "";
}
//insert updated row and cells
let length = myLibrary.length;
const length = myLibrary.length;
for (let i = 0; i < 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;
const row = table.insertRow(-1);
const titleCell = row.insertCell(0);
const authorCell = row.insertCell(1);
const pagesCell = row.insertCell(2);
const wasReadCell = row.insertCell(3);
const deleteCell = row.insertCell(4);
titleCell.textContent = myLibrary[i].title;
authorCell.textContent = myLibrary[i].author;
pagesCell.textContent = myLibrary[i].pages;

//add and wait for action for read/unread button
let changeBut = document.createElement("button");
changeBut.id = i;
const changeBut = document.createElement("button");
changeBut.className = "btn btn-success";
wasReadCell.appendChild(changeBut);
let readStatus = "";
if (myLibrary[i].check == false) {
readStatus = "Yes";
} else {
readStatus = "No";
}
changeBut.innerText = readStatus;
changeBut.innerText = myLibrary[i].check ? "Yes" : "No";

changeBut.addEventListener("click", function () {
myLibrary[i].check = !myLibrary[i].check;
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 delButton = document.createElement("button");
deleteCell.appendChild(delButton);
delButton.className = "btn btn-warning";
delButton.innerHTML = "Delete";
delButton.addEventListener("click", function () {
const deletedTitle = myLibrary[i].title;
myLibrary.splice(i, 1);
render();
alert(`You've deleted title: ${deletedTitle}`);
});
}
}