Skip to content

Commit aadf229

Browse files
committed
Refactor render function for improved efficiency and clarity in cell creation
1 parent eea5314 commit aadf229

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

debugging/book-library/script.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,25 @@ function Book(titleValue, authorValue, pagesNumber, isRead) {
6161

6262
function render() {
6363
const table = document.getElementById("display");
64+
const tbody = table.querySelector("tbody");
65+
tbody.innerHTML = ""; // clear previous rows
6466

65-
const tbody = table.querySelector('tbody');
66-
tbody.innerHTML = ''; // clears all rows efficiently
67-
6867
myLibrary.forEach((book, i) => {
69-
const row = tbody.insertRow(); // insert row into tbody
68+
const row = tbody.insertRow();
7069

71-
row.insertCell(0).textContent = book.bookTitle;
72-
row.insertCell(1).textContent = book.author;
73-
row.insertCell(2).textContent = book.pages;
74-
75-
// read/unread toggle
70+
// Create ALL CELLS first (like original code)
71+
const titleCell = row.insertCell(0);
72+
const authorCell = row.insertCell(1);
73+
const pagesCell = row.insertCell(2);
7674
const wasReadCell = row.insertCell(3);
75+
const deleteCell = row.insertCell(4);
76+
77+
// Fill text cells
78+
titleCell.textContent = book.bookTitle;
79+
authorCell.textContent = book.author;
80+
pagesCell.textContent = book.pages;
81+
82+
// --- Read/Unread Button ---
7783
const changeBut = document.createElement("button");
7884
changeBut.className = "btn btn-success";
7985
changeBut.textContent = book.check ? "Yes" : "No";
@@ -84,8 +90,7 @@ function render() {
8490
render();
8591
});
8692

87-
// delete button
88-
const deleteCell = row.insertCell(4);
93+
// --- Delete Button ---
8994
const delBut = document.createElement("button");
9095
delBut.className = "btn btn-warning";
9196
delBut.textContent = "Delete";

0 commit comments

Comments
 (0)