Skip to content

Commit e7f2081

Browse files
committed
Debugged the code
1 parent 326c8a5 commit e7f2081

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

debugging/book-library/script.js

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
let myLibrary = [];
22

33
window.addEventListener("load", function (e) {
4-
populateStorage();
4+
if (localStorage.getItem("myLibrary")) {
5+
myLibrary = JSON.parse(localStorage.getItem("myLibrary"));
6+
} else {
7+
populateStorage();
8+
}
59
render();
610
});
7-
811
function populateStorage() {
912
if (myLibrary.length == 0) {
10-
let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true);
11-
let book2 = new Book(
12-
"The Old Man and the Sea",
13-
"Ernest Hemingway",
14-
"127",
15-
true
16-
);
13+
let book1 = new Book("Robinson Crusoe", "Daniel Defoe", "252", true);
14+
let book2 = new Book("The Old Man and the Sea", "Ernest Hemingway", "127", true);
1715
myLibrary.push(book1);
1816
myLibrary.push(book2);
1917
render();
@@ -31,36 +29,40 @@ function submit() {
3129
if (
3230
title.value == null ||
3331
title.value == "" ||
32+
author.value == null ||
33+
author.value == "" ||
3434
pages.value == null ||
3535
pages.value == ""
3636
) {
3737
alert("Please fill all fields!");
3838
return false;
3939
} else {
40-
let book = new Book(title.value, title.value, pages.value, check.checked);
41-
library.push(book);
40+
let book = new Book(title.value, author.value, pages.value, check.checked);
41+
myLibrary.push(book);
4242
render();
4343
}
4444
}
4545

46-
function Book(title, author, pages, check) {
47-
this.title = title;
48-
this.author = author;
49-
this.pages = pages;
50-
this.check = check;
46+
class Book {
47+
constructor(title, author, pages, check) {
48+
this.title = title;
49+
this.author = author;
50+
this.pages = pages;
51+
this.check = check;
52+
}
5153
}
5254

5355
function render() {
5456
let table = document.getElementById("display");
5557
let rowsNumber = table.rows.length;
5658
//delete old table
57-
for (let n = rowsNumber - 1; n > 0; n-- {
59+
for (let n = rowsNumber - 1; n > 0; n--) {
5860
table.deleteRow(n);
5961
}
6062
//insert updated row and cells
6163
let length = myLibrary.length;
6264
for (let i = 0; i < length; i++) {
63-
let row = table.insertRow(1);
65+
let row = table.insertRow(-1);
6466
let titleCell = row.insertCell(0);
6567
let authorCell = row.insertCell(1);
6668
let pagesCell = row.insertCell(2);
@@ -76,7 +78,7 @@ function render() {
7678
changeBut.className = "btn btn-success";
7779
wasReadCell.appendChild(changeBut);
7880
let readStatus = "";
79-
if (myLibrary[i].check == false) {
81+
if (myLibrary[i].check == true) {
8082
readStatus = "Yes";
8183
} else {
8284
readStatus = "No";
@@ -89,15 +91,15 @@ function render() {
8991
});
9092

9193
//add delete button to every row and render again
92-
let delButton = document.createElement("button");
94+
let delBut = document.createElement("button");
9395
delBut.id = i + 5;
9496
deleteCell.appendChild(delBut);
9597
delBut.className = "btn btn-warning";
9698
delBut.innerHTML = "Delete";
97-
delBut.addEventListener("clicks", function () {
99+
delBut.addEventListener("click", function () {
98100
alert(`You've deleted title: ${myLibrary[i].title}`);
99101
myLibrary.splice(i, 1);
100102
render();
101103
});
102104
}
103-
}
105+
}

0 commit comments

Comments
 (0)