11let myLibrary = [ ] ;
22
3- window . addEventListener ( "load" , function ( e ) {
3+ window . addEventListener ( "load" , function ( ) {
44 populateStorage ( ) ;
5- //render();
65} ) ;
76
87function populateStorage ( ) {
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 ) ;
13- myLibrary . push ( book2 ) ;
14- render ( ) ;
8+ if ( myLibrary . length === 0 ) {
9+ const book1 = new Book ( "Robison Crusoe" , "Daniel Defoe" , 252 , true ) ;
10+ const book2 = new Book ( "The Old Man and the Sea" , "Ernest Hemingway" , 127 , true ) ;
11+ myLibrary . push ( book1 , book2 ) ;
1512 }
13+ render ( ) ;
1614}
1715
1816const titleEl = document . getElementById ( "title" ) ;
1917const authorEl = document . getElementById ( "author" ) ;
2018const pagesEl = document . getElementById ( "pages" ) ;
2119const checkEl = document . getElementById ( "check" ) ;
2220
23- //check the right input from forms and if its ok -> add the new book (object in array)
24- //via Book function and start render function
25- function submit ( ) {
21+ // Add book from form
22+ function submitBook ( event ) {
23+ event . preventDefault ( ) ; // Prevent page reload
24+
2625 const titleVal = titleEl . value . trim ( ) ;
2726 const authorVal = authorEl . value . trim ( ) ;
2827 const pagesVal = Number ( pagesEl . value ) ;
29- if (
30- ! titleVal ||
31- ! pagesVal ||
32- ! authorVal
33- ) {
28+
29+ if ( ! titleVal || ! authorVal || ! pagesVal ) {
3430 alert ( "Please fill all fields!" ) ;
35- return false ;
36- } else {
37- //fix: change title.value to author.value
38- let book = new Book ( titleVal , authorVal , pagesVal , checkEl . checked ) ;
39- //Fix: change library.push(book) to myLibrary.push(book).
40- myLibrary . push ( book ) ;
41- render ( ) ;
31+ return ;
4232 }
33+
34+ const book = new Book ( titleVal , authorVal , pagesVal , checkEl . checked ) ;
35+ myLibrary . push ( book ) ;
36+ render ( ) ;
37+
38+ // Clear form
39+ titleEl . value = "" ;
40+ authorEl . value = "" ;
41+ pagesEl . value = "" ;
42+ checkEl . checked = false ;
4343}
4444
4545function Book ( title , author , pages , check ) {
@@ -50,59 +50,39 @@ function Book(title, author, pages, check) {
5050}
5151
5252function render ( ) {
53- let table = document . getElementById ( "display" ) ;
54- let rowsNumber = table . rows . length ;
55- //delete old table
56- // Fix : add a ) to the end of the for loop condition
57- // to avoid syntax error.
53+ const table = document . getElementById ( "display" ) ;
5854 const tbody = table . querySelector ( "tbody" ) ;
59- tbody . innerHTML = "" ;
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 . innerText = myLibrary [ i ] . title ;
70- authorCell . innerText = myLibrary [ i ] . author ;
71- pagesCell . innerText = myLibrary [ i ] . pages ;
55+ tbody . innerHTML = "" ; // clear old rows
7256
73- //add and wait for action for read/unread button
74- let readBtn = document . createElement ( "button" ) ;
75- readBtn . id = `readBtn-${ i } ` ;
76- readBtn . className = "btn btn-success" ;
77- wasReadCell . appendChild ( readBtn ) ;
78- let readStatus = "" ;
79- // Fix: change myLibrary[i].check to myLibrary[i].check == true
80- // to check if the book is read or not.
81- if ( myLibrary [ i ] . check == true ) {
82- readStatus = "Yes" ;
83- } else {
84- readStatus = "No" ;
85- }
86- readBtn . innerText = readStatus ;
57+ myLibrary . forEach ( ( book , i ) => {
58+ const row = tbody . insertRow ( ) ;
59+ row . insertCell ( 0 ) . textContent = book . title ;
60+ row . insertCell ( 1 ) . textContent = book . author ;
61+ row . insertCell ( 2 ) . textContent = book . pages ;
8762
88- readBtn . addEventListener ( "click" , function ( ) {
89- myLibrary [ i ] . check = ! myLibrary [ i ] . check ;
63+ // Read/Unread button
64+ const readCell = row . insertCell ( 3 ) ;
65+ const readBtn = document . createElement ( "button" ) ;
66+ readBtn . className = "btn btn-success" ;
67+ readBtn . textContent = book . check ? "Yes" : "No" ;
68+ readBtn . addEventListener ( "click" , ( ) => {
69+ book . check = ! book . check ;
9070 render ( ) ;
9171 } ) ;
72+ readCell . appendChild ( readBtn ) ;
9273
93- //add delete button to every row and render again
94- // Fix :
95- // change delBut to delButton because we declare it.
96- // The event listener is "clicks" instead of "click".
97- let deleteBtn = document . createElement ( "button" ) ;
98- deleteBtn . id = `deleteBtn-${ i } ` ;
99- deleteCell . appendChild ( deleteBtn ) ;
100- deleteBtn . className = "btn btn-warning" ;
101- deleteBtn . innerHTML = "Delete" ;
102- deleteBtn . addEventListener ( "click" , function ( ) {
103- alert ( `You've deleted title: ${ myLibrary [ i ] . title } ` ) ;
104- myLibrary . splice ( i , 1 ) ;
105- render ( ) ;
74+ // Delete button
75+ const delCell = row . insertCell ( 4 ) ;
76+ const delBtn = document . createElement ( "button" ) ;
77+ delBtn . className = "btn btn-warning" ;
78+ delBtn . textContent = "Delete" ;
79+ delBtn . addEventListener ( "click" , ( ) => {
80+ if ( confirm ( `Are you sure you want to delete "${ book . title } "?` ) ) {
81+ myLibrary . splice ( i , 1 ) ;
82+ render ( ) ;
83+ }
10684 } ) ;
107- }
85+ delCell . appendChild ( delBtn ) ;
86+ } ) ;
10887}
88+
0 commit comments