Skip to content

Commit c2e4d17

Browse files
committed
made a start on the refactor stuck on how to clear the table at top of render function
1 parent b0f2a10 commit c2e4d17

2 files changed

Lines changed: 56 additions & 78 deletions

File tree

‎debugging/book-library/index.html‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,6 @@ <h1>Library</h1>
9191
</tbody>
9292
</table>
9393

94-
<script src="script.js"></script>
94+
<script src="script.js" ></script>
9595
</body>
9696
</html>

‎debugging/book-library/script.js‎

Lines changed: 55 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,126 +1,104 @@
11
let myLibrary = [];
22

3-
window.addEventListener("load", function (e) {
3+
window.addEventListener("load", function () {
44
populateStorage();
5-
render();
65
});
76

8-
9-
10-
117
function populateStorage() {
128
if (myLibrary.length === 0) {
13-
let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true);
14-
let book2 = new Book(
9+
const book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true);
10+
const book2 = new Book(
1511
"The Old Man and the Sea",
16-
"Ernest Hemingway", "5", true);
12+
"Ernest Hemingway",
13+
"500",
14+
true
15+
);
1716
myLibrary.push(book1, book2);
18-
1917

20-
render();
18+
render();
2119
}
2220
}
2321

24-
25-
2622
//check the right input from forms and if its ok -> add the new book (object in array)
2723
//via Book function and start render function
2824

2925
function submit() {
30-
const title = document.getElementById("title").value;
31-
const author = document.getElementById("author").value;
32-
const pages = document.getElementById("pages").value;
33-
const check = document.getElementById("check").checked;
34-
if (
35-
36-
title == "" ||
37-
pages == ""||
38-
author == ""
39-
)
40-
41-
{
26+
const titleInput = document.getElementById("title").value;
27+
const authorInput = document.getElementById("author").value;
28+
const pagesInput = document.getElementById("pages").value;
29+
const checkInput = document.getElementById("check").checked;
30+
if (titleInput == "" || pagesInput == "" || authorInput == "") {
4231
alert("Please fill all fields!");
4332
return true;
4433
} else {
45-
let book = new Book(title, author, pages, check);
34+
let book = new Book(titleInput, authorInput, pagesInput, checkInput);
4635
myLibrary.push(book);
4736
render();
48-
//clear the input values
49-
50-
37+
//clear the input values
5138
}
5239
}
5340

54-
function Book(title, author, pages, check) {
55-
this.title = title;
56-
this.author = author;
57-
this.pages = pages;
58-
this.check = check;
41+
function Book(titleInput, authorInput, pagesInput, checkInput) {
42+
this.title = titleInput;
43+
this.author = authorInput;
44+
this.pages = pagesInput;
45+
this.check = checkInput;
5946
}
6047

6148
function render() {
62-
let table = document.getElementById("display");
63-
let rowsNumber = table.rows.length;
49+
const table = document.getElementById("display");
50+
const rowsNumber = table.rows.length;
6451
//delete old table
65-
for (let n = rowsNumber - 1; n > 0; n--) {
66-
table.deleteRow(n);
67-
68-
}
6952

53+
for (let n = rowsNumber - 1; n > 0; n--) {
54+
table.deleteRow(n);
55+
}
7056

71-
7257
//insert updated row and cells
73-
let length = myLibrary.length ;
58+
const length = myLibrary.length;
7459
for (let i = 0; i <= length; i++) {
75-
76-
let row = table.insertRow(1);
77-
let titleCell = row.insertCell(0);
78-
let authorCell = row.insertCell(1);
79-
let pagesCell = row.insertCell(2);
80-
let wasReadCell = row.insertCell(3);
81-
let deleteCell = row.insertCell(4);
60+
const row = table.insertRow(1);
61+
const titleCell = row.insertCell(0);
62+
const authorCell = row.insertCell(1);
63+
const pagesCell = row.insertCell(2);
64+
const wasReadCell = row.insertCell(3);
65+
const deleteCell = row.insertCell(4);
8266
titleCell.innerHTML = myLibrary[i].title;
8367
authorCell.innerHTML = myLibrary[i].author;
8468
pagesCell.innerHTML = myLibrary[i].pages;
85-
86-
//deleteCell.innerHTML = delBut[i]
8769

88-
89-
70+
//deleteCell.innerHTML = button[i]
9071

9172
//add and wait for action for read/unread button
92-
let changeBut = document.createElement("button");
93-
changeBut.id = i;
94-
changeBut.className = "btn btn-success";
95-
wasReadCell.appendChild(changeBut);
73+
const changeButton = document.createElement("button");
74+
changeButton.id = i;
75+
changeButton.className = "btn btn-success";
76+
wasReadCell.appendChild(changeButton);
9677
let readStatus = "";
97-
if (myLibrary[i].check == true) {
98-
readStatus = "Yes";
99-
} else {
100-
readStatus = "No";
101-
}
102-
changeBut.innerText = readStatus;
10378

104-
changeBut.addEventListener("click", function () {
79+
myLibrary[i].check == true
80+
? (readStatus = "Yes")
81+
: (readStatus = "No");
82+
83+
changeButton.innerText = readStatus;
10584

106-
if( myLibrary[i].check == true){
107-
myLibrary[i].check = false;
108-
}
109-
else {
110-
myLibrary[i].check = true;
111-
}
85+
changeButton.addEventListener("click", function () {
86+
myLibrary[i].check == true
87+
? (myLibrary[i].check = false)
88+
: (myLibrary[i].check = true);
11289

11390
render();
114-
});
91+
});
11592
//add delete button to every row and render again
116-
let delBut = document.createElement("button");
117-
delBut.id = i ;
118-
deleteCell.appendChild(delBut);
119-
delBut.className = "btn btn-warning";
120-
delBut.innerHTML = "Delete";
121-
delBut.addEventListener("click", function () {
93+
const button = document.createElement("button");
94+
button.id = i;
95+
deleteCell.appendChild(button);
96+
button.className = "btn btn-warning";
97+
button.innerHTML = "Delete";
98+
button.addEventListener("click", function () {
12299
alert(`You've deleted title: ${myLibrary[i].title}`);
123100
myLibrary.splice(i, 1);
124101
render();
125-
})};
102+
});
126103
}
104+
}

0 commit comments

Comments
 (0)