Skip to content

Commit b809cf0

Browse files
committed
refactor(book-library): apply best practices and style guide
- Rename code variables to descriptive element suffixes - Optimize render function to clear table efficiently - Update comments to concise British English
1 parent 9b0672f commit b809cf0

File tree

1 file changed

+37
-38
lines changed

1 file changed

+37
-38
lines changed

debugging/book-library/script.js

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
const title = document.getElementById("title");
2-
const author = document.getElementById("author");
3-
const pages = document.getElementById("pages");
4-
const check = document.getElementById("check");
1+
const titleInput = document.getElementById("title");
2+
const authorInput = document.getElementById("author");
3+
const pagesInput = document.getElementById("pages");
4+
const readCheckbox = document.getElementById("check");
55
const table = document.getElementById("display");
66
const submitBtn = document.getElementById("submit-book-btn");
77

@@ -19,7 +19,7 @@ function populateStorage() {
1919
if (storedLibrary) {
2020
myLibrary = JSON.parse(storedLibrary);
2121
} else {
22-
// Default books if nothing in storage
22+
// Initialises default books if storage empty
2323
let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true);
2424
let book2 = new Book(
2525
"The Old Man and the Sea",
@@ -37,27 +37,29 @@ function saveStorage() {
3737
localStorage.setItem("myLibrary", JSON.stringify(myLibrary));
3838
}
3939

40-
//check the right input from forms and if its ok -> add the new book (object in array)
41-
//via Book function and start render function
40+
// Validates input and adds new book
4241
function addBook(e) {
43-
// Prevent form submission if we wrap this in a form later,
44-
// though currently it's just a button. Good practice.
4542
if (e) e.preventDefault();
4643

47-
if (!title.value || !author.value || !pages.value) {
44+
if (!titleInput.value || !authorInput.value || !pagesInput.value) {
4845
alert("Please fill all fields!");
4946
return false;
5047
} else {
51-
let book = new Book(title.value, author.value, pages.value, check.checked);
48+
let book = new Book(
49+
titleInput.value,
50+
authorInput.value,
51+
pagesInput.value,
52+
readCheckbox.checked
53+
);
5254
myLibrary.push(book);
5355
saveStorage();
5456
render();
5557

56-
// Clear inputs
57-
title.value = "";
58-
author.value = "";
59-
pages.value = "";
60-
check.checked = false;
58+
// Clears inputs
59+
titleInput.value = "";
60+
authorInput.value = "";
61+
pagesInput.value = "";
62+
readCheckbox.checked = false;
6163
}
6264
}
6365

@@ -69,54 +71,51 @@ function Book(title, author, pages, check) {
6971
}
7072

7173
function render() {
72-
let rowsNumber = table.rows.length;
73-
//delete old table
74-
for (let n = rowsNumber - 1; n > 0; n--) {
75-
table.deleteRow(n);
76-
}
77-
//insert updated row and cells
74+
// Clears table body efficiently
75+
const tbody = table.querySelector("tbody");
76+
tbody.innerHTML = "";
77+
78+
// Inserts updated row and cells
7879
let length = myLibrary.length;
7980
for (let i = 0; i < length; i++) {
8081
// Insert at the end of the table
81-
let row = table.insertRow(-1);
82+
let row = tbody.insertRow(-1);
8283
let titleCell = row.insertCell(0);
8384
let authorCell = row.insertCell(1);
8485
let pagesCell = row.insertCell(2);
8586
let wasReadCell = row.insertCell(3);
8687
let deleteCell = row.insertCell(4);
8788

88-
// SECURITY: Use textContent to prevent XSS
89+
// Uses textContent to prevent XSS
8990
titleCell.textContent = myLibrary[i].title;
9091
authorCell.textContent = myLibrary[i].author;
9192
pagesCell.textContent = myLibrary[i].pages;
9293

93-
//add and wait for action for read/unread button
94-
let changeBut = document.createElement("button");
95-
// Removed arbitrary index-based ID
96-
changeBut.className = "btn btn-success";
97-
wasReadCell.appendChild(changeBut);
94+
// Toggles read status
95+
let readBtn = document.createElement("button");
96+
readBtn.className = "btn btn-success";
97+
wasReadCell.appendChild(readBtn);
9898

9999
let readStatus = "";
100100
if (myLibrary[i].check == false) {
101101
readStatus = "No";
102102
} else {
103103
readStatus = "Yes";
104104
}
105-
changeBut.textContent = readStatus;
105+
readBtn.textContent = readStatus;
106106

107-
changeBut.addEventListener("click", function () {
107+
readBtn.addEventListener("click", function () {
108108
myLibrary[i].check = !myLibrary[i].check;
109109
saveStorage();
110110
render();
111111
});
112112

113-
//add delete button to every row and render again
114-
let delButton = document.createElement("button");
115-
// Removed arbitrary index-based ID
116-
deleteCell.appendChild(delButton);
117-
delButton.className = "btn btn-warning";
118-
delButton.textContent = "Delete";
119-
delButton.addEventListener("click", function () {
113+
// Deletes book
114+
let deleteBtn = document.createElement("button");
115+
deleteCell.appendChild(deleteBtn);
116+
deleteBtn.className = "btn btn-warning";
117+
deleteBtn.textContent = "Delete";
118+
deleteBtn.addEventListener("click", function () {
120119
if (confirm(`Are you sure you want to delete: ${myLibrary[i].title}?`)) {
121120
myLibrary.splice(i, 1);
122121
saveStorage();

0 commit comments

Comments
 (0)