Skip to content

Commit 4da0a58

Browse files
committed
fixing bugs in js code
1 parent 3a422e9 commit 4da0a58

1 file changed

Lines changed: 10 additions & 14 deletions

File tree

debugging/book-library/script.js

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ window.addEventListener("load", function (e) {
77

88
function populateStorage() {
99
if (myLibrary.length == 0) {
10-
let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true);
10+
let book1 = new Book("Robinson Crusoe", "Daniel Defoe", "252", true);
1111
let book2 = new Book(
1212
"The Old Man and the Sea",
1313
"Ernest Hemingway",
@@ -16,7 +16,6 @@ function populateStorage() {
1616
);
1717
myLibrary.push(book1);
1818
myLibrary.push(book2);
19-
render();
2019
}
2120
}
2221

@@ -31,14 +30,16 @@ function submit() {
3130
if (
3231
title.value == null ||
3332
title.value == "" ||
33+
author.value == null ||
34+
author.value == "" ||
3435
pages.value == null ||
3536
pages.value == ""
3637
) {
3738
alert("Please fill all fields!");
3839
return false;
3940
} else {
40-
let book = new Book(title.value, title.value, pages.value, check.checked);
41-
library.push(book);
41+
let book = new Book(title.value, author.value, pages.value, check.checked);
42+
myLibrary.push(book);
4243
render();
4344
}
4445
}
@@ -54,7 +55,7 @@ function render() {
5455
let table = document.getElementById("display");
5556
let rowsNumber = table.rows.length;
5657
//delete old table
57-
for (let n = rowsNumber - 1; n > 0; n-- {
58+
for (let n = rowsNumber - 1; n > 0; n--) {
5859
table.deleteRow(n);
5960
}
6061
//insert updated row and cells
@@ -75,26 +76,21 @@ function render() {
7576
changeBut.id = i;
7677
changeBut.className = "btn btn-success";
7778
wasReadCell.appendChild(changeBut);
78-
let readStatus = "";
79-
if (myLibrary[i].check == false) {
80-
readStatus = "Yes";
81-
} else {
82-
readStatus = "No";
83-
}
84-
changeBut.innerText = readStatus;
79+
80+
changeBut.innerText = myLibrary[i].check ? "Yes" : "No";
8581

8682
changeBut.addEventListener("click", function () {
8783
myLibrary[i].check = !myLibrary[i].check;
8884
render();
8985
});
9086

9187
//add delete button to every row and render again
92-
let delButton = document.createElement("button");
88+
let delBut = document.createElement("button");
9389
delBut.id = i + 5;
9490
deleteCell.appendChild(delBut);
9591
delBut.className = "btn btn-warning";
9692
delBut.innerHTML = "Delete";
97-
delBut.addEventListener("clicks", function () {
93+
delBut.addEventListener("click", function () {
9894
alert(`You've deleted title: ${myLibrary[i].title}`);
9995
myLibrary.splice(i, 1);
10096
render();

0 commit comments

Comments
 (0)