Skip to content
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
8 changes: 4 additions & 4 deletions debugging/book-library/index.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!DOCTYPE html>
<!doctype html>
<html>
<head>
<title> </title>
Expand Down Expand Up @@ -59,13 +59,13 @@ <h1>Library</h1>
class="form-check-input"
id="check"
value=""
/>Read
/>completed
</label>
<input
type="submit"
value="Submit"
class="btn btn-primary"
onclick="submit();"
onclick="submit()"
/>
</div>
</div>
Expand All @@ -76,7 +76,7 @@ <h1>Library</h1>
<th>Title</th>
<th>Author</th>
<th>Number of Pages</th>
<th>Read</th>
<th>Completed</th>
<th></th>
</tr>
</thead>
Expand Down
57 changes: 38 additions & 19 deletions debugging/book-library/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ let myLibrary = [];

window.addEventListener("load", function (e) {
populateStorage();
render();
});

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

myLibrary.push(book1);
myLibrary.push(book2);
render();
Expand All @@ -36,25 +36,46 @@ function submit() {
) {
alert("Please fill all fields!");
return false;
} else {
let book = new Book(title.value, title.value, pages.value, check.checked);
library.push(book);
render();
}

let book = new Book(title.value, author.value, pages.value, check.checked);

if (isBookInLibrary(book)) {
alert("This book is already in the library!");
return false;
}
myLibrary.push(book);
resetInputFields();
render();
}

function isBookInLibrary(newBook) {
return myLibrary.some(
(old) =>
old.title.toLowerCase() === newBook.title.toLowerCase() &&
old.author.toLowerCase() === newBook.author.toLowerCase()
);
}

function resetInputFields() {
title.value = "";
author.value = "";
pages.value = "";
check.checked = false;
}

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

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 @@ -72,29 +93,27 @@ function render() {

//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) {
if (myLibrary[i].completed == true) {
readStatus = "Yes";
} else {
readStatus = "No";
}
changeBut.innerText = readStatus;

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