Skip to content
Open
Changes from 1 commit
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
55 changes: 20 additions & 35 deletions debugging/book-library/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@ window.addEventListener("load", function (e) {
function populateStorage() {
if (myLibrary.length == 0) {
let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true);
let book2 = new Book(
"The Old Man and the Sea",
"Ernest Hemingway",
"127",
true
);
let book2 = new Book("The Old Man and the Sea","Ernest Hemingway","127",true);
myLibrary.push(book1);
myLibrary.push(book2);
render();
Expand All @@ -28,19 +23,17 @@ 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 == ""
) {
if (title.value === "" ||author.value === "" ||pages.value === "") {
alert("Please fill all fields!");
return false;
} else {
let book = new Book(title.value, title.value, pages.value, check.checked);
library.push(book);
}
let book = new Book(title.value, author.value, pages.value, check.checked);
myLibrary.push(book);
render();
}
title.value = "";
author.value = "";
pages.value = "";
check.checked = false;
}

function Book(title, author, pages, check) {
Expand All @@ -54,12 +47,11 @@ 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
let length = myLibrary.length;
for (let i = 0; i < length; i++) {
for (let i = 0; i < myLibrary.length; i++) {
let row = table.insertRow(1);
let titleCell = row.insertCell(0);
let authorCell = row.insertCell(1);
Expand All @@ -72,32 +64,25 @@ 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) {
readStatus = "Yes";
} else {
readStatus = "No";
}
changeBut.innerText = readStatus;

changeBut.addEventListener("click", function () {
changeBut.innerText = myLibrary[i].check ? "Yes" : "No";
changeBut.addEventListener ("click", function () {
myLibrary[i].check = !myLibrary[i].check;
render();
});

wasReadCell.appendChild(changeBut);
//add delete button to every row and render again
let delButton = document.createElement("button");
delBut.id = i + 5;
deleteCell.appendChild(delBut);
let delBut = document.createElement("button");
delBut.className = "btn btn-warning";
delBut.innerHTML = "Delete";
delBut.addEventListener("clicks", function () {

delBut.addEventListener("click", function () {
alert(`You've deleted title: ${myLibrary[i].title}`);
myLibrary.splice(i, 1);
render();
});

deleteCell.appendChild(delBut);
}
}