File tree Expand file tree Collapse file tree 1 file changed +23
-10
lines changed
Expand file tree Collapse file tree 1 file changed +23
-10
lines changed Original file line number Diff line number Diff line change @@ -25,22 +25,35 @@ const author = document.getElementById("author");
2525const pages = document . getElementById ( "pages" ) ;
2626const 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+
3029function 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
4659function Book ( title , author , pages , check ) {
You can’t perform that action at this time.
0 commit comments