Skip to content

Commit febabaf

Browse files
committed
clean the code
1 parent 09081dd commit febabaf

File tree

1 file changed

+21
-27
lines changed

1 file changed

+21
-27
lines changed

debugging/book-library/script.js

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

33
window.addEventListener("load", function (e) {
4-
populateStorage(); // just if myLibrary is empty put two book on it with a specific details
4+
populateStorage();
55
render();
66
});
77

88
//just one thing need to check
99
function populateStorage() {
10-
if (myLibrary.length == 0) { //why there is just == not ===
10+
if (myLibrary.length === 0) { //why there is just == not ===
1111
let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true);
1212
let book2 = new Book(
1313
"The Old Man and the Sea",
@@ -20,8 +20,8 @@ function populateStorage() {
2020
render();
2121
}
2222
}
23-
// clear associated with HTML
24-
const title = document.getElementById("title");
23+
24+
const title = document.getElementById("title");
2525
const author = document.getElementById("author");
2626
const pages = document.getElementById("pages");
2727
const check = document.getElementById("check");
@@ -30,24 +30,18 @@ const check = document.getElementById("check");
3030
//via Book function and start render function
3131
function submit() {
3232
//if any of the information is missing show an alert
33-
if (!title.value || !author.value ||!pages.value)
34-
// == checks both null and undefined . === only null
35-
// why you use this way and where author check??
36-
// if(!title.value||author.value|| page.value) is more clear and readable
37-
38-
{
39-
alert("Please fill all fields!"); // alert is a built-in JS function
40-
return false;
33+
if (!title.value || !author.value || !pages.value) {
34+
alert("Please fill all fields!");
35+
36+
return false;
4137
} else {
4238
let book = new Book(title.value, author.value, pages.value, check.checked);
43-
myLibrary.push(book); // ???? the var called MyLibrary bro
44-
render(); // tell now where as submit event maybe later I will check
39+
myLibrary.push(book);
40+
render();
4541
}
4642
}
47-
let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true); ///why i have this in populteStorage and here
4843

49-
//function to create a new book as an object
50-
function Book(title, author, pages, check) {
44+
function Book(title, author, pages, check) {
5145
this.title = title;
5246
this.author = author;
5347
this.pages = pages;
@@ -58,16 +52,16 @@ function render() {
5852
let table = document.getElementById("display");
5953
let rowsNumber = table.rows.length;
6054
//delete old table
61-
for (let n = rowsNumber - 1; n > 0; n--) { //n>0 because we will delete all rows except the first one header
62-
table.deleteRow(n); //.deleteRow is a built-in JS function it remove the row by index
55+
for (let n = rowsNumber - 1; n > 0; n--) {
56+
table.deleteRow(n);
6357
}
6458
//insert updated row and cells
6559
let length = myLibrary.length;
6660
for (let i = 0; i < length; i++) {
6761
let row = table.insertRow(1); // why (1) not i+1 insert <tr> with four <td>
6862
let titleCell = row.insertCell(0); // the table.rows and row.cells are HTMLCollection,not a real array, but they behave like array(indexed,length) . they are DOM collections
6963
let authorCell = row.insertCell(1);
70-
let pagesCell = row.insertCell(2);
64+
let pagesCell = row.insertCell(2);
7165
let wasReadCell = row.insertCell(3);
7266
let deleteCell = row.insertCell(4); // it is like insert for <td> </td>
7367
titleCell.innerHTML = myLibrary[i].title; //filling the cells why innerHTML
@@ -78,32 +72,32 @@ function render() {
7872
let changeBut = document.createElement("button");
7973
changeBut.id = i; // give the button the index of the book object
8074
changeBut.className = "btn btn-success";
81-
wasReadCell.appendChild(changeBut);
75+
wasReadCell.appendChild(changeBut);
8276
let readStatus = "";
83-
if (myLibrary[i].check == true) { // why not ===
77+
if (myLibrary[i].check === true) {
8478
readStatus = "Yes";
8579
} else {
8680
readStatus = "No";
8781
}
88-
changeBut.innerText = readStatus; // fill the button with yes or no
82+
changeBut.innerText = readStatus;
8983

9084
changeBut.addEventListener("click", function () {
91-
myLibrary[i].check = !myLibrary[i].check; //when we click will change yes to no and no to yes
85+
myLibrary[i].check = !myLibrary[i].check;
9286
render();
9387
});
9488

9589
//add delete button to every row and render again
96-
//delButton
90+
9791
let delButton = document.createElement("button");
98-
delButton.id = i + 5; // I think there is an issue here if we have more than five books
92+
delButton.id = i + 5;
9993
deleteCell.appendChild(delButton);
10094
delButton.className = "btn btn-warning";
10195
delButton.innerHTML = "Delete";
10296
delButton.addEventListener("click", function () { //
10397
alert(`You've deleted title: ${myLibrary[i].title}`);
10498
myLibrary.splice(i, 1);
10599
render();
106-
//we do rendering after each change to UI
100+
107101
});
108102
}
109103
}

0 commit comments

Comments
 (0)