Skip to content

Commit 0b088a8

Browse files
committed
feat(validate): require author and enforce pages as positive integer"
1 parent 6d9d84a commit 0b088a8

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

debugging/book-library/script.js

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,35 @@ const author = document.getElementById("author");
2525
const pages = document.getElementById("pages");
2626
const check = document.getElementById("check");
2727

28-
//check the right input from forms and if its ok -> add the new book (object in array)
29-
//via Book function and start render function
28+
3029
function submit() {
30+
3131
if (
32-
title.value == null ||
33-
title.value == "" ||
34-
pages.value == null ||
35-
pages.value == ""
32+
!title.value?.trim() ||
33+
!author.value?.trim() ||
34+
!pages.value?.trim()
3635
) {
3736
alert("Please fill all fields!");
3837
return false;
39-
} else {
40-
let book = new Book(title.value, author.value, pages.value, check.checked);
41-
myLibrary.push(book);
42-
render();
4338
}
39+
40+
const pagesNum = Number(pages.value);
41+
if (!Number.isInteger(pagesNum) || pagesNum < 1) {
42+
alert("Pages must be a positive whole number.");
43+
return false;
44+
}
45+
46+
// create and add the book
47+
const book = new Book(
48+
title.value.trim(),
49+
author.value.trim(),
50+
pagesNum,
51+
check.checked
52+
);
53+
myLibrary.push(book);
54+
render();
55+
return true;
56+
}
4457
}
4558

4659
function Book(title, author, pages, check) {

0 commit comments

Comments
 (0)