diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html
index 23acfa71..2bcf29ed 100644
--- a/debugging/book-library/index.html
+++ b/debugging/book-library/index.html
@@ -1,4 +1,4 @@
-
+
@@ -59,13 +59,13 @@ Library
class="form-check-input"
id="check"
value=""
- />Read
+ />completed
@@ -76,7 +76,7 @@ Library
Title |
Author |
Number of Pages |
- Read |
+ Completed |
|
diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js
index 75ce6c1d..71283f6b 100644
--- a/debugging/book-library/script.js
+++ b/debugging/book-library/script.js
@@ -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();
@@ -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
@@ -72,11 +93,10 @@ 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";
@@ -84,17 +104,16 @@ function render() {
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();