11let myLibrary = [ ] ;
2+ class Book {
3+ constructor ( title , author , pages , hasRead ) {
4+ this . title = title ;
5+ this . author = author ;
6+ this . pages = pages ;
7+ this . hasRead = Boolean ( hasRead ) ;
8+ }
9+ }
210
3- window . addEventListener ( "load" , function ( ) {
4- populateStorage ( ) ;
5- render ( ) ;
6- document . getElementById ( "submitBtn" ) . addEventListener ( "click" , submit ) ;
11+ // Initial Setup
12+ window . addEventListener ( "load" , ( ) => {
13+ seedInitialBooks ( ) ;
14+ renderLibrary ( ) ;
15+ document . getElementById ( "submitBtn" ) . addEventListener ( "click" , handleSubmit ) ;
716} ) ;
817
9- function populateStorage ( ) {
18+ // Add starter books if library is empty
19+ function seedInitialBooks ( ) {
1020 if ( myLibrary . length === 0 ) {
1121 myLibrary . push ( new Book ( "Robinson Crusoe" , "Daniel Defoe" , 252 , true ) ) ;
1222 myLibrary . push ( new Book ( "The Old Man and the Sea" , "Ernest Hemingway" , 127 , true ) ) ;
1323 }
1424}
1525
26+ // Form Inputs
1627const titleInput = document . getElementById ( "title" ) ;
1728const authorInput = document . getElementById ( "author" ) ;
1829const pagesInput = document . getElementById ( "pages" ) ;
1930const readCheckBox = document . getElementById ( "check" ) ;
2031
21- function submit ( ) {
32+ // Form Submission
33+ function handleSubmit ( ) {
2234 const titleVal = titleInput . value . trim ( ) ;
2335 const authorVal = authorInput . value . trim ( ) ;
2436 const pagesVal = pagesInput . value . trim ( ) ;
2537
26- if ( ! titleVal || ! authorVal || ! pagesVal ) {
38+ if ( ! validateInputs ( titleVal , authorVal , pagesVal ) ) return ;
39+
40+ const newBook = new Book (
41+ titleVal ,
42+ authorVal ,
43+ Number ( pagesVal ) ,
44+ readCheckBox . checked
45+ ) ;
46+
47+ myLibrary . push ( newBook ) ;
48+ resetForm ( ) ;
49+ renderLibrary ( ) ;
50+ }
51+
52+ // Validate input values
53+ function validateInputs ( title , author , pages ) {
54+ if ( ! title || ! author || ! pages ) {
2755 alert ( "Please fill all fields!" ) ;
2856 return false ;
2957 }
3058
31- if ( isNaN ( Number ( pagesVal ) ) || Number ( pagesVal ) <= 0 ) {
59+ const pagesNum = Number ( pages ) ;
60+
61+ if ( isNaN ( pagesNum ) || pagesNum <= 0 ) {
3262 alert ( "Please enter a valid number of pages!" ) ;
3363 return false ;
3464 }
3565
36- const book = new Book ( titleVal , authorVal , Number ( pagesVal ) , readCheckBox . checked ) ;
37- myLibrary . push ( book ) ;
66+ return true ;
67+ }
3868
69+ // Clear form after successful submission
70+ function resetForm ( ) {
3971 titleInput . value = "" ;
4072 authorInput . value = "" ;
4173 pagesInput . value = "" ;
4274 readCheckBox . checked = false ;
43-
44- render ( ) ;
45- return true ;
46- }
47-
48- function Book ( title , author , pages , hasRead ) {
49- this . title = title ;
50- this . author = author ;
51- this . pages = pages ;
52- this . hasRead = Boolean ( hasRead ) ;
5375}
5476
55- function render ( ) {
77+ // Rendering
78+ function renderLibrary ( ) {
5679 const tbody = document . querySelector ( "#display tbody" ) ;
5780 tbody . innerHTML = "" ;
5881
59- myLibrary . forEach ( ( book , i ) => {
82+ myLibrary . forEach ( ( book , index ) => {
6083 const row = document . createElement ( "tr" ) ;
6184
6285 row . innerHTML = `
@@ -67,32 +90,35 @@ function render() {
6790 <td></td>
6891 ` ;
6992
93+ // Read Button
7094 const readButton = document . createElement ( "button" ) ;
71- if ( book . hasRead ) {
72- readButton . className = "btn btn-success" ;
73- readButton . textContent = "Yes" ;
74- } else {
75- readButton . className = "btn btn-danger" ;
76- readButton . textContent = "No" ;
77- }
78- readButton . addEventListener ( "click" , ( ) => {
79- book . hasRead = ! book . hasRead ;
80- render ( ) ;
81- } ) ;
95+ readButton . textContent = book . hasRead ? "Yes" : "No" ;
96+ readButton . className = book . hasRead ? "btn btn-success" : "btn btn-danger" ;
97+ readButton . addEventListener ( "click" , ( ) => toggleReadStatus ( index ) ) ;
8298
8399 row . children [ 3 ] . appendChild ( readButton ) ;
84100
101+ // Delete Button
85102 const deleteButton = document . createElement ( "button" ) ;
86103 deleteButton . className = "btn btn-warning" ;
87104 deleteButton . textContent = "Delete" ;
88- deleteButton . addEventListener ( "click" , ( ) => {
89- alert ( `You deleted: ${ book . title } ` ) ;
90- myLibrary . splice ( i , 1 ) ;
91- render ( ) ;
92- } ) ;
105+ deleteButton . addEventListener ( "click" , ( ) => deleteBook ( index ) ) ;
93106
94107 row . children [ 4 ] . appendChild ( deleteButton ) ;
95108
96109 tbody . appendChild ( row ) ;
97110 } ) ;
98111}
112+
113+ // Toggle read/unread status
114+ function toggleReadStatus ( index ) {
115+ myLibrary [ index ] . hasRead = ! myLibrary [ index ] . hasRead ;
116+ renderLibrary ( ) ;
117+ }
118+
119+ // Delete a book from the library
120+ function deleteBook ( index ) {
121+ alert ( `You deleted: ${ myLibrary [ index ] . title } ` ) ;
122+ myLibrary . splice ( index , 1 ) ;
123+ renderLibrary ( ) ;
124+ }
0 commit comments