Skip to content

Commit 5c86e22

Browse files
committed
Refactor page number validation
1 parent 05f6ca0 commit 5c86e22

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

debugging/book-library/index.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,4 @@ <h1>Library</h1>
5050
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
5151
<script src="script.js" type="module"></script>
5252
</body>
53-
5453
</html>

debugging/book-library/script.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ function addBook() {
3939
return;
4040
}
4141

42-
if (isNaN(Number(pages)) || Number(pages) <= 0) {
43-
showError("Pages must be a positive number.");
42+
const pageNum = Number(pages);
43+
if (!Number.isInteger(pageNum) || pageNum <= 0) {
44+
showError("Pages must be a positive whole number.");
4445
return;
4546
}
4647

@@ -49,12 +50,13 @@ function addBook() {
4950
return;
5051
}
5152

52-
const book = new Book(title, author, Number(pages), read);
53+
const book = new Book(title, author, pageNum, read);
5354
myLibrary.push(book);
5455
clearForm();
5556
render();
5657
}
5758

59+
5860
document.getElementById("submitBtn").addEventListener("click", addBook);
5961

6062
function clearForm() {

0 commit comments

Comments
 (0)