11let myLibrary = [ ] ;
22
3- window . addEventListener ( "load" , function ( e ) {
3+ window . addEventListener ( "load" , function ( ) {
44 populateStorage ( ) ;
55 render ( ) ;
66} ) ;
77
88function populateStorage ( ) {
9- if ( myLibrary . length == 0 ) {
10- let book1 = new Book ( "Robison Crusoe" , "Daniel Defoe" , "252" , true ) ;
11- let book2 = new Book (
9+ if ( myLibrary . length === 0 ) {
10+ const book1 = new Book (
11+ "Robinson Crusoe" ,
12+ "Daniel Defoe" ,
13+ "252" ,
14+ true
15+ ) ;
16+
17+ const book2 = new Book (
1218 "The Old Man and the Sea" ,
1319 "Ernest Hemingway" ,
1420 "127" ,
1521 true
1622 ) ;
23+
1724 myLibrary . push ( book1 ) ;
1825 myLibrary . push ( book2 ) ;
19- render ( ) ;
2026 }
2127}
2228
@@ -25,22 +31,34 @@ const author = document.getElementById("author");
2531const pages = document . getElementById ( "pages" ) ;
2632const check = document . getElementById ( "check" ) ;
2733
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
3034function submit ( ) {
3135 if (
32- title . value == null ||
33- title . value == "" ||
34- pages . value == null ||
35- pages . value == ""
36+ title . value . trim ( ) === "" ||
37+ author . value . trim ( ) === "" ||
38+ pages . value . trim ( ) === ""
3639 ) {
3740 alert ( "Please fill all fields!" ) ;
3841 return false ;
39- } else {
40- let book = new Book ( title . value , title . value , pages . value , check . checked ) ;
41- library . push ( book ) ;
42- render ( ) ;
4342 }
43+
44+ const book = new Book (
45+ title . value . trim ( ) ,
46+ author . value . trim ( ) ,
47+ pages . value ,
48+ check . checked
49+ ) ;
50+
51+ myLibrary . push ( book ) ;
52+
53+ render ( ) ;
54+
55+ // Clear the form after adding a book
56+ title . value = "" ;
57+ author . value = "" ;
58+ pages . value = "" ;
59+ check . checked = false ;
60+
61+ return true ;
4462}
4563
4664function Book ( title , author , pages , check ) {
@@ -51,53 +69,58 @@ function Book(title, author, pages, check) {
5169}
5270
5371function render ( ) {
54- let table = document . getElementById ( "display" ) ;
55- let rowsNumber = table . rows . length ;
56- //delete old table
57- for ( let n = rowsNumber - 1 ; n > 0 ; n -- {
72+ const table = document . getElementById ( "display" ) ;
73+ const rowsNumber = table . rows . length ;
74+
75+ // Remove old book rows but keep the heading row
76+ for ( let n = rowsNumber - 1 ; n > 0 ; n -- ) {
5877 table . deleteRow ( n ) ;
5978 }
60- //insert updated row and cells
61- let length = myLibrary . length ;
62- for ( let i = 0 ; i < length ; i ++ ) {
63- let row = table . insertRow ( 1 ) ;
64- let titleCell = row . insertCell ( 0 ) ;
65- let authorCell = row . insertCell ( 1 ) ;
66- let pagesCell = row . insertCell ( 2 ) ;
67- let wasReadCell = row . insertCell ( 3 ) ;
68- let deleteCell = row . insertCell ( 4 ) ;
69- titleCell . innerHTML = myLibrary [ i ] . title ;
70- authorCell . innerHTML = myLibrary [ i ] . author ;
71- pagesCell . innerHTML = myLibrary [ i ] . pages ;
72-
73- //add and wait for action for read/unread button
74- let changeBut = document . createElement ( "button" ) ;
75- changeBut . id = i ;
76- changeBut . className = "btn btn-success" ;
77- wasReadCell . appendChild ( changeBut ) ;
78- let readStatus = "" ;
79- if ( myLibrary [ i ] . check == false ) {
80- readStatus = "Yes" ;
81- } else {
82- readStatus = "No" ;
83- }
84- changeBut . innerText = readStatus ;
85-
86- changeBut . addEventListener ( "click" , function ( ) {
79+
80+ for ( let i = 0 ; i < myLibrary . length ; i ++ ) {
81+ const row = table . insertRow ( - 1 ) ;
82+
83+ const titleCell = row . insertCell ( 0 ) ;
84+ const authorCell = row . insertCell ( 1 ) ;
85+ const pagesCell = row . insertCell ( 2 ) ;
86+ const wasReadCell = row . insertCell ( 3 ) ;
87+ const deleteCell = row . insertCell ( 4 ) ;
88+
89+ titleCell . textContent = myLibrary [ i ] . title ;
90+ authorCell . textContent = myLibrary [ i ] . author ;
91+ pagesCell . textContent = myLibrary [ i ] . pages ;
92+
93+ // Read / unread button
94+ const changeButton = document . createElement ( "button" ) ;
95+
96+ changeButton . className = "btn btn-success" ;
97+
98+ changeButton . textContent = myLibrary [ i ] . check
99+ ? "Yes"
100+ : "No" ;
101+
102+ changeButton . addEventListener ( "click" , function ( ) {
87103 myLibrary [ i ] . check = ! myLibrary [ i ] . check ;
88104 render ( ) ;
89105 } ) ;
90106
91- //add delete button to every row and render again
92- let delButton = document . createElement ( "button" ) ;
93- delBut . id = i + 5 ;
94- deleteCell . appendChild ( delBut ) ;
95- delBut . className = "btn btn-warning" ;
96- delBut . innerHTML = "Delete" ;
97- delBut . addEventListener ( "clicks" , function ( ) {
98- alert ( `You've deleted title: ${ myLibrary [ i ] . title } ` ) ;
107+ wasReadCell . appendChild ( changeButton ) ;
108+
109+ // Delete button
110+ const deleteButton = document . createElement ( "button" ) ;
111+
112+ deleteButton . className = "btn btn-warning" ;
113+ deleteButton . textContent = "Delete" ;
114+
115+ deleteButton . addEventListener ( "click" , function ( ) {
116+ alert (
117+ `You've deleted title: ${ myLibrary [ i ] . title } `
118+ ) ;
119+
99120 myLibrary . splice ( i , 1 ) ;
100121 render ( ) ;
101122 } ) ;
123+
124+ deleteCell . appendChild ( deleteButton ) ;
102125 }
103- }
126+ }
0 commit comments