Skip to content

ITP_JAN_25 | HASAN DEMIROZ | Data-Flows | Book-Library #217

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
7 changes: 0 additions & 7 deletions debugging/book-library/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,6 @@ <h1>Library</h1>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>

Expand Down
109 changes: 55 additions & 54 deletions debugging/book-library/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,31 @@ const author = document.getElementById("author");
const pages = document.getElementById("pages");
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!");

const titleValue = title.value.trim();
const authorValue = author.value.trim();
const pagesValue = pages.value.trim();

if (titleValue === "" || authorValue === "") {
alert("Title and author cannot be empty or just spaces!");
return false;
} else {
let book = new Book(title.value, title.value, pages.value, check.checked);
library.push(book);
render();
}

if (!/^\d+$/.test(pagesValue) || parseInt(pagesValue, 10) <= 0) {
alert("Please enter a valid positive number for pages!");
return false;
}

let book = new Book(titleValue, authorValue, pagesValue, check.checked);
myLibrary.push(book);
render();

title.value = "";
author.value = "";
pages.value = "";
check.checked = false;
return false;
}

function Book(title, author, pages, check) {
Expand All @@ -51,53 +60,45 @@ 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);
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 () {
const table = document.getElementById("display");
const tbody = table.getElementsByTagName("tbody")[0];

tbody.innerHTML = "";

// Insert updated rows
for (let i = 0; i < myLibrary.length; i++) {
let row = tbody.insertRow();

let titleCell = row.insertCell();
titleCell.textContent = myLibrary[i].title;

let authorCell = row.insertCell();
authorCell.textContent = myLibrary[i].author;

let pagesCell = row.insertCell();
pagesCell.textContent = myLibrary[i].pages;

// Read/Unread button cell
let wasReadCell = row.insertCell();
let readStatusButton = document.createElement("button");
readStatusButton.className = "btn btn-success";
readStatusButton.innerText = myLibrary[i].check ? "Yes" : "No";
readStatusButton.addEventListener("click", function () {
myLibrary[i].check = !myLibrary[i].check;
render();
});
wasReadCell.appendChild(readStatusButton);

//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 () {
// Delete button cell
let deleteCell = row.insertCell();
let deleteButton = document.createElement("button");
deleteButton.className = "btn btn-warning";
deleteButton.innerText = "Delete";
deleteButton.addEventListener("click", function () {
alert(`You've deleted title: ${myLibrary[i].title}`);
myLibrary.splice(i, 1);
render();
});
deleteCell.appendChild(deleteButton);
}
}
}