Skip to content
Closed
Show file tree
Hide file tree
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
13 changes: 5 additions & 8 deletions debugging/book-library/index.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<title> </title>
<meta
charset="utf-8"
name="viewport"
content="width=device-width, initial-scale=1.0"
/>
<title>Book Library Project | (Adiyah Farhan)</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
Expand All @@ -31,15 +28,15 @@ <h1>Library</h1>
<div class="form-group">
<label for="title">Title:</label>
<input
type="title"
type="text"
class="form-control"
id="title"
name="title"
required
/>
<label for="author">Author: </label>
<input
type="author"
type="text"
class="form-control"
id="author"
name="author"
Expand Down
117 changes: 73 additions & 44 deletions debugging/book-library/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,97 +7,126 @@ window.addEventListener("load", function (e) {

function populateStorage() {
if (myLibrary.length == 0) {
let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true);
let book1 = new Book("Robison Crusoe", "Daniel Defoe", 252, true);
let book2 = new Book(
"The Old Man and the Sea",
"Ernest Hemingway",
"127",
127,
true
);
myLibrary.push(book1);
myLibrary.push(book2);
render();
}
}

const title = document.getElementById("title");
const author = document.getElementById("author");
const pages = document.getElementById("pages");
const check = document.getElementById("check");
const titleElement = document.getElementById("title");
const authorElement = document.getElementById("author");
const pagesElement = document.getElementById("pages");
const checkElement = 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 == ""
) {
if (!titleElement.value || !authorElement.value || !pagesElement.value) {
alert("Please fill all fields!");
return false;
} else if (!titleElement.value.match(/^(?=.*[a-zA-Z])[a-zA-Z ]+$/)) {
alert("Please enter a valid book title!");
return false;
} else if (!authorElement.value.match(/^(?=.*[a-zA-Z])[a-zA-Z ]+$/)) {
alert("Please enter a valid author name!");
return false;
} else if (!pagesElement.value.match(/^[0-9]+$/)) {
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);
let book = new Book(
titleElement.value,
authorElement.value,
pagesElement.value,
checkElement.checked
);

myLibrary.push(book);
render();
}
}

function Book(title, author, pages, check) {
this.title = title;
this.author = author;
this.pages = pages;
this.title = title.trim(); //remove leading/trailing spaces
this.author = author.trim();
this.pages = Number(pages); // Ensure pages is a number
this.check = check;
}

function render() {
let table = document.getElementById("display");
let rowsNumber = table.rows.length;
//Method 1: To clear the table
//let table = document.getElementById("display");
//delete all rows except the first one (header)
//let rowsNumber = table.rows.length;
//delete old table
for (let n = rowsNumber - 1; n > 0; n-- {
table.deleteRow(n);
}
//for (let n = rowsNumber - 1; n > 0; n--) {
//table.deleteRow(n);
//}

//Method 2: To clear the table in one operation
//replaceChildren() is a modern method to clear all children of an element
let tableBody = document.querySelector("tbody");
tableBody.replaceChildren(); // Clear the table body

//insert updated row and cells
let length = myLibrary.length;
for (let i = 0; i < length; i++) {
let row = table.insertRow(1);
let row = tableBody.insertRow(0); // Insert a new row at the beginning of the table body
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;

//innerHTML was replaced with textContent for cleaner text handling / code semantics.
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;
changeBut.className = "btn btn-success";
wasReadCell.appendChild(changeBut);
let readButton = document.createElement("button");

// readButton.id = i; /
// No need for an ID here, as we can use the index directly
readButton.className = "btn btn-success";
wasReadCell.appendChild(readButton);
let readStatus = "";
if (myLibrary[i].check == false) {
readStatus = "Yes";
} else {
readStatus = "No";
} else {
readStatus = "Yes";
}
changeBut.innerText = readStatus;
readButton.innerText = readStatus;
Comment on lines 99 to +105
Copy link
Contributor

Choose a reason for hiding this comment

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

This could be a good opportunity to practice using the ? : conditional operator. Can you rewrite the code on lines 99-105 as a single statement?


changeBut.addEventListener("click", function () {
readButton.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}`);
myLibrary.splice(i, 1);
render();
let deleteButton = document.createElement("button");

// No need to set an ID for the delete button, as we can use the index directly
//deleteButton.id = i + 5;
deleteCell.appendChild(deleteButton);
deleteButton.className = "btn btn-warning";
deleteButton.textContent = "Delete";
deleteButton.addEventListener("click", function () {
const deletedTitle = myLibrary[i].title; //save the title of the book to be deleted
myLibrary.splice(i, 1); // Remove the book from the library array
render(); // update the table after deletion

// Use setTimeout to ensure the alert is shown after the render
// This is useful to ensure the UI is updated before showing the alert
setTimeout(() => {
alert(`You've deleted title: ${deletedTitle}`);
}, 0.5);
});
}
}
Loading