11let myLibrary = [ ] ;
22
3- window . addEventListener ( "load" , function ( e ) {
3+ window . addEventListener ( "load" , function ( ) {
44 populateStorage ( ) ;
5+ render ( ) ;
56} ) ;
67
78function populateStorage ( ) {
8- if ( myLibrary . length == 0 ) {
9- let book1 = new Book ( "Robison Crusoe" , "Daniel Defoe" , "252" , true ) ;
10- let book2 = new Book (
11- "The Old Man and the Sea" ,
12- "Ernest Hemingway" ,
13- "127" ,
14- true
15- ) ;
16- myLibrary . push ( book1 ) ;
17- myLibrary . push ( book2 ) ;
9+ if ( myLibrary . length === 0 ) {
10+ let book1 = new Book ( "Robison Crusoe" , "Daniel Defoe" , 252 , true ) ;
11+ let book2 = new Book ( "The Old Man and the Sea" , "Ernest Hemingway" , 127 , true ) ;
12+ myLibrary . push ( book1 , book2 ) ;
1813 }
1914}
2015
@@ -23,30 +18,27 @@ const author = document.getElementById("author");
2318const pages = document . getElementById ( "pages" ) ;
2419const check = document . getElementById ( "check" ) ;
2520
26- // Check the right input from forms and add the new book
2721function submit ( ) {
28- if (
29- title . value == null ||
30- title . value . trim ( ) === "" ||
31- author . value == null ||
32- author . value . trim ( ) === "" ||
33- pages . value == null ||
34- pages . value === ""
35- ) {
22+ const titleValue = title . value . trim ( ) ;
23+ const authorValue = author . value . trim ( ) ;
24+ const pagesValue = pages . value . trim ( ) ;
25+
26+ if ( ! titleValue || ! authorValue || ! pagesValue ) {
3627 alert ( "Please fill all fields!" ) ;
3728 return false ;
3829 }
3930
4031 // Prevent numbers in Author field
41- if ( / \d / . test ( author . value ) ) {
32+ if ( / \d / . test ( authorValue ) ) {
4233 alert ( "Author name cannot contain numbers!" ) ;
4334 return false ;
4435 }
4536
46- let book = new Book ( title . value , author . value , pages . value , check . checked ) ;
37+ let book = new Book ( titleValue , authorValue , Number ( pagesValue ) , check . checked ) ;
4738 myLibrary . push ( book ) ;
4839 render ( ) ;
4940
41+ // Reset form
5042 title . value = "" ;
5143 author . value = "" ;
5244 pages . value = "" ;
@@ -61,59 +53,47 @@ function Book(title, author, pages, check) {
6153}
6254
6355function render ( ) {
64- let table = document . getElementById ( "display" ) ;
65- let rowsNumber = table . rows . length ;
66- // Delete old table rows
67- for ( let n = rowsNumber - 1 ; n > 0 ; n -- ) {
68- table . deleteRow ( n ) ;
69- }
70- // Insert updated rows
71- let length = myLibrary . length ;
72- for ( let i = 0 ; i < length ; i ++ ) {
73- let row = table . insertRow ( 1 ) ;
74- let titleCell = row . insertCell ( 0 ) ;
75- let authorCell = row . insertCell ( 1 ) ;
76- let pagesCell = row . insertCell ( 2 ) ;
77- let wasReadCell = row . insertCell ( 3 ) ;
78- let deleteCell = row . insertCell ( 4 ) ;
79- titleCell . innerHTML = myLibrary [ i ] . title ;
80- authorCell . innerHTML = myLibrary [ i ] . author ;
81- pagesCell . innerHTML = myLibrary [ i ] . pages ;
82-
83- // Add read/unread button
84- let changeBut = document . createElement ( "button" ) ;
85- changeBut . id = i ;
86- changeBut . className = "btn btn-success" ;
87- wasReadCell . appendChild ( changeBut ) ;
88-
89- let readStatus = "" ;
90- if ( myLibrary [ i ] . check === true ) {
91- readStatus = "Yes" ;
92- } else {
93- readStatus = "No" ;
94- }
95- changeBut . innerText = readStatus ;
96-
97- changeBut . addEventListener ( "click" , function ( ) {
56+ const table = document . getElementById ( "display" ) ;
57+ const tbody = table . querySelector ( "tbody" ) ;
58+
59+ // Clear all existing rows
60+ tbody . innerHTML = "" ;
61+
62+ myLibrary . forEach ( ( book , i ) => {
63+ const row = tbody . insertRow ( ) ;
64+
65+ row . insertCell ( 0 ) . textContent = book . title ;
66+ row . insertCell ( 1 ) . textContent = book . author ;
67+ row . insertCell ( 2 ) . textContent = book . pages ;
68+
69+ // Read/unread button
70+ const wasReadCell = row . insertCell ( 3 ) ;
71+ const readToggleButton = document . createElement ( "button" ) ;
72+ readToggleButton . className = "btn btn-success" ;
73+ readToggleButton . textContent = book . check ? "Yes" : "No" ;
74+ wasReadCell . appendChild ( readToggleButton ) ;
75+
76+ readToggleButton . addEventListener ( "click" , ( ) => {
9877 myLibrary [ i ] . check = ! myLibrary [ i ] . check ;
9978 render ( ) ;
10079 } ) ;
10180
102- // Add delete button
103- let delButton = document . createElement ( "button" ) ;
104- delButton . id = i + 5 ;
105- deleteCell . appendChild ( delButton ) ;
106- delButton . className = "btn btn-warning" ;
107- delButton . innerHTML = "Delete" ;
108- delButton . addEventListener ( "click" , function ( ) {
109- alert ( `You've deleted title: ${ myLibrary [ i ] . title } ` ) ;
110- myLibrary . splice ( i , 1 ) ;
111- render ( ) ;
81+ // Delete button
82+ const deleteCell = row . insertCell ( 4 ) ;
83+ const deleteButton = document . createElement ( "button" ) ;
84+ deleteButton . className = "btn btn-warning" ;
85+ deleteButton . textContent = "Delete" ;
86+ deleteCell . appendChild ( deleteButton ) ;
87+
88+ deleteButton . addEventListener ( "click" , ( ) => {
89+ myLibrary . splice ( i , 1 ) ; // delete immediately
90+ render ( ) ; // update the table
91+ alert ( `Deleted "${ book . title } " successfully.` ) ; // optional confirmation
11292 } ) ;
113- }
93+ } ) ;
11494}
11595
11696// Make header row toggle the form
117- document . querySelector ( ".thead-dark tr" ) . addEventListener ( "click" , function ( e ) {
97+ document . querySelector ( ".thead-dark tr" ) . addEventListener ( "click" , function ( ) {
11898 $ ( "#demo" ) . collapse ( "toggle" ) ;
11999} ) ;
0 commit comments