@@ -28,21 +28,29 @@ const checkInput = document.getElementById("check");
2828//via Book function and start render function
2929function handleSubmit ( event ) {
3030 event . preventDefault ( ) ;
31+ let titleValue = titleInput . value . trim ( ) ;
32+ let authorValue = authorInput . value . trim ( ) ;
33+ let pagesValue = Number ( pagesInput . value . trim ( ) ) ;
34+ let read = checkInput . checked ;
3135 if (
32- ! titleInput . value . trim ( ) ||
33- ! authorInput . value . trim ( ) ||
34- pagesInput . value <= 0
36+ ! titleValue ||
37+ ! authorValue ||
38+ ! Number . isInteger ( pagesValue ) ||
39+ Number ( pagesValue ) <= 0
3540 ) {
36- alert ( "Please fill all fields!" ) ;
41+ alert ( "Please fill all fields! Page count must be a whole number above 0. " ) ;
3742 return false ;
3843 } else {
39- let book = new Book ( String ( titleInput . value ) , String ( authorInput . value ) , Number ( pagesInput . value ) , checkInput . checked ) ;
44+ let book = new Book ( titleValue , authorValue , pagesValue , read ) ;
4045 myLibrary . push ( book ) ;
4146 resetAddNewBook ( )
4247 render ( ) ;
4348 }
4449}
4550
51+ const bookForm = document . getElementById ( "book-form" )
52+ bookForm . addEventListener ( "submit" , ( event ) => handleSubmit ( event ) ) ;
53+
4654function resetAddNewBook ( ) {
4755 document . getElementById ( "book-form" ) . reset ( )
4856}
@@ -58,7 +66,7 @@ class Book {
5866
5967function render ( ) {
6068 //delete old table
61- let tableBody = document . getElementById ( "display-body" )
69+ const tableBody = document . getElementById ( "display-body" )
6270 tableBody . innerHTML = '' ;
6371 //insert updated row and cells
6472 myLibrary . forEach ( ( book , index ) => {
0 commit comments