Skip to content

Commit db2bff7

Browse files
committed
wrote a conditional statement in a single statement
1 parent 333f269 commit db2bff7

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

debugging/book-library/script.js

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,30 @@ const check = 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 addBook() {
30-
if (!title.value.trim() || !author.value.trim() || !pages.value.trim()) {
31-
alert("Please fill all fields!");
32-
return false;
30+
const sanitizedTitle = title.value.trim();
31+
const sanitizedAuthor = author.value.trim();
32+
const sanitizedPages = Number(pages.value);
33+
const isRead = check.checked;
34+
35+
if (!sanitizedTitle || !sanitizedAuthor) {
36+
alert("Title and author cannot be empty.");
37+
return;
3338
}
34-
35-
let pagesNumber = parseInt(pages.value, 10);
36-
if (isNaN(pagesNumber)) {
37-
alert("Page count must be a number!");
38-
return false;
39+
40+
if (!Number.isInteger(sanitizedPages) || sanitizedPages <= 0) {
41+
alert("Pages must be a positive whole number.");
42+
return;
3943
}
40-
let book = new Book(title.value.trim(), author.value.trim(), pagesNumber, check.checked);
44+
45+
const book = new Book(
46+
sanitizedTitle,
47+
sanitizedAuthor,
48+
sanitizedPages,
49+
isRead
50+
);
51+
4152
myLibrary.push(book);
4253
render();
43-
4454
}
4555

4656
function Book(title, author, pages, check) {
@@ -76,13 +86,11 @@ function render() {
7686
toggleReadBtn.dataset.index = i;
7787
toggleReadBtn.className = "btn btn-success";
7888
wasReadCell.appendChild(toggleReadBtn);
79-
let readStatus = "";
80-
if (myLibrary[i].check == true) {
81-
readStatus = "Yes";
82-
} else {
83-
readStatus = "No";
84-
}
89+
90+
let readStatus = myLibrary[i].check ? "Yes" : "No";
8591
toggleReadBtn.innerText = readStatus;
92+
changeBut.innerText = readStatus;
93+
8694

8795
toggleReadBtn.addEventListener("click", function () {
8896
myLibrary[i].check = !myLibrary[i].check;

0 commit comments

Comments
 (0)