Skip to content

Commit 9e9749a

Browse files
committed
Resolved all known bugs
1 parent 3a422e9 commit 9e9749a

1 file changed

Lines changed: 29 additions & 23 deletions

File tree

debugging/book-library/script.js

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ window.addEventListener("load", function (e) {
66
});
77

88
function populateStorage() {
9-
if (myLibrary.length == 0) {
9+
if (myLibrary.length === 0) {
1010
let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true);
1111
let book2 = new Book(
1212
"The Old Man and the Sea",
@@ -28,33 +28,39 @@ const check = document.getElementById("check");
2828
//check the right input from forms and if its ok -> add the new book (object in array)
2929
//via Book function and start render function
3030
function submit() {
31-
if (
32-
title.value == null ||
33-
title.value == "" ||
34-
pages.value == null ||
35-
pages.value == ""
36-
) {
31+
if (title.value === "" || author.value === "" || pages.value === "") {
3732
alert("Please fill all fields!");
3833
return false;
3934
} else {
40-
let book = new Book(title.value, title.value, pages.value, check.checked);
41-
library.push(book);
35+
let book = new Book(
36+
title.value,
37+
author.value,
38+
String(pages.value),
39+
check.checked
40+
);
41+
myLibrary.push(book);
4242
render();
43+
title.value = "";
44+
author.value = "";
45+
pages.value = "";
46+
check.checked = false;
4347
}
4448
}
4549

46-
function Book(title, author, pages, check) {
47-
this.title = title;
48-
this.author = author;
49-
this.pages = pages;
50-
this.check = check;
50+
class Book {
51+
constructor(title, author, pages, check) {
52+
this.title = title;
53+
this.author = author;
54+
this.pages = pages;
55+
this.check = check;
56+
}
5157
}
5258

5359
function render() {
5460
let table = document.getElementById("display");
5561
let rowsNumber = table.rows.length;
5662
//delete old table
57-
for (let n = rowsNumber - 1; n > 0; n-- {
63+
for (let n = rowsNumber - 1; n > 0; n--) {
5864
table.deleteRow(n);
5965
}
6066
//insert updated row and cells
@@ -76,10 +82,10 @@ function render() {
7682
changeBut.className = "btn btn-success";
7783
wasReadCell.appendChild(changeBut);
7884
let readStatus = "";
79-
if (myLibrary[i].check == false) {
80-
readStatus = "Yes";
81-
} else {
85+
if (myLibrary[i].check === false) {
8286
readStatus = "No";
87+
} else {
88+
readStatus = "Yes";
8389
}
8490
changeBut.innerText = readStatus;
8591

@@ -90,11 +96,11 @@ function render() {
9096

9197
//add delete button to every row and render again
9298
let delButton = document.createElement("button");
93-
delBut.id = i + 5;
94-
deleteCell.appendChild(delBut);
95-
delBut.className = "btn btn-warning";
96-
delBut.innerHTML = "Delete";
97-
delBut.addEventListener("clicks", function () {
99+
delButton.id = i + 5;
100+
deleteCell.appendChild(delButton);
101+
delButton.className = "btn btn-warning";
102+
delButton.innerHTML = "Delete";
103+
delButton.addEventListener("click", function (e) {
98104
alert(`You've deleted title: ${myLibrary[i].title}`);
99105
myLibrary.splice(i, 1);
100106
render();

0 commit comments

Comments
 (0)