Skip to content

Commit 3809f61

Browse files
committed
Refactor submit function to improve readability and use consistent variable names
1 parent 584176d commit 3809f61

File tree

1 file changed

+37
-30
lines changed

1 file changed

+37
-30
lines changed

debugging/book-library/script.js

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,32 @@ const checkInput = document.getElementById("check");
2727
//check the right input from forms and if its ok -> add the new book (object in array)
2828
//via Book function and start render function
2929
function submit() {
30-
return !title.value.trim() || !author.value.trim() || !pages.value.trim()
31-
? (alert("Please fill all fields!"), false)
32-
: function () {
33-
let book = new Book(
34-
title.value,
35-
author.value,
36-
pages.value,
37-
check.checked
38-
);
39-
myLibrary.push(book);
40-
render();
41-
//clear user input data after storing the information
42-
title.value = "";
43-
author.value = "";
44-
pages.value = "";
45-
check.checked = false;
46-
};
47-
}
30+
if (
31+
!titleInput.value.trim() ||
32+
!authorInput.value.trim() ||
33+
!pagesInput.value.trim()
34+
) {
35+
alert("Please fill all fields!");
36+
return false;
37+
} else {
38+
myLibrary.push(
39+
new Book(
40+
titleInput.value.trim(),
41+
authorInput.value.trim(),
42+
Number(pagesInput.value),
43+
checkInput.checked
44+
)
45+
);
4846

47+
render();
48+
//clear user input data after storing the information
49+
titleInput.value = "";
50+
authorInput.value = "";
51+
pagesInput.value = "";
52+
checkInput.checked = false;
53+
return true;
54+
}
55+
}
4956
function Book(title, author, pages, check) {
5057
this.title = title;
5158
this.author = author;
@@ -74,30 +81,30 @@ function render() {
7481
pagesCell.innerHTML = myLibrary[i].pages;
7582

7683
//add and wait for action for read/unread button
77-
let changeBut = document.createElement("button");
78-
changeBut.id = i;
79-
changeBut.className = "btn btn-success";
80-
wasReadCell.appendChild(changeBut);
84+
let toggleReadBTN = document.createElement("button");
85+
toggleReadBTN.id = i;
86+
toggleReadBTN.className = "btn btn-success";
87+
wasReadCell.appendChild(toggleReadBTN);
8188
let readStatus = "";
8289
if (myLibrary[i].check == false) {
8390
readStatus = "No";
8491
} else {
8592
readStatus = "Yes";
8693
}
87-
changeBut.innerText = readStatus;
94+
toggleReadBTN.innerText = readStatus;
8895

89-
changeBut.addEventListener("click", function () {
96+
toggleReadBTN.addEventListener("click", function () {
9097
myLibrary[i].check = !myLibrary[i].check;
9198
render();
9299
});
93100

94101
//add delete button to every row and render again
95-
let delButton = document.createElement("button");
96-
delButton.id = i + 5;
97-
deleteCell.appendChild(delButton);
98-
delButton.className = "btn btn-warning";
99-
delButton.innerHTML = "Delete";
100-
delButton.addEventListener("click", function () {
102+
let deleteBtn = document.createElement("button");
103+
deleteBtn.id = i + 5;
104+
deleteCell.appendChild(deleteBtn);
105+
deleteBtn.className = "btn btn-warning";
106+
deleteBtn.innerHTML = "Delete";
107+
deleteBtn.addEventListener("click", function () {
101108
alert(`You've deleted title: ${myLibrary[i].title}`);
102109
myLibrary.splice(i, 1);
103110
render();

0 commit comments

Comments
 (0)