11let myLibrary = [ ] ;
22
3- window . addEventListener ( "load" , function ( e ) {
3+ window . addEventListener ( "load" , function ( ) {
44 populateStorage ( ) ;
55 render ( ) ;
66} ) ;
77
88function populateStorage ( ) {
99 if ( myLibrary . length == 0 ) {
10- let book1 = new Book ( "Robison Crusoe" , "Daniel Defoe" , "252" , true ) ;
11- let book2 = new Book (
12- "The Old Man and the Sea" ,
13- "Ernest Hemingway" ,
14- "127" ,
15- true
16- ) ;
10+ let book1 = new Book ( "Robinson Crusoe" , "Daniel Defoe" , 252 , true ) ;
11+ let book2 = new Book ( "The Old Man and the Sea" , "Ernest Hemingway" , 127 , true ) ;
1712 myLibrary . push ( book1 ) ;
1813 myLibrary . push ( book2 ) ;
19- render ( ) ;
2014 }
2115}
2216
@@ -25,79 +19,72 @@ const author = document.getElementById("author");
2519const pages = document . getElementById ( "pages" ) ;
2620const check = document . getElementById ( "check" ) ;
2721
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
3022function submit ( ) {
31- if (
32- title . value == null ||
33- title . value == "" ||
34- pages . value == null ||
35- pages . value == ""
36- ) {
23+ if ( title . value === "" || author . value === "" || pages . value === "" ) {
3724 alert ( "Please fill all fields!" ) ;
38- return false ;
39- } else {
40- let book = new Book ( title . value , title . value , pages . value , check . checked ) ;
41- library . push ( book ) ;
42- render ( ) ;
25+ return ;
4326 }
27+
28+ let book = new Book ( title . value , author . value , pages . value , check . checked ) ;
29+ myLibrary . push ( book ) ;
30+
31+ // clear form
32+ title . value = "" ;
33+ author . value = "" ;
34+ pages . value = "" ;
35+ check . checked = false ;
36+
37+ render ( ) ;
4438}
4539
46- function Book ( title , author , pages , check ) {
40+ function Book ( title , author , pages , read ) {
4741 this . title = title ;
4842 this . author = author ;
4943 this . pages = pages ;
50- this . check = check ;
44+ this . read = read ; // renamed from "check" (more readable)
5145}
5246
5347function render ( ) {
5448 let table = document . getElementById ( "display" ) ;
55- let rowsNumber = table . rows . length ;
56- //delete old table
57- for ( let n = rowsNumber - 1 ; n > 0 ; n -- {
58- table . deleteRow ( n ) ;
49+
50+ // delete old rows
51+ while ( table . rows . length > 1 ) {
52+ table . deleteRow ( 1 ) ;
5953 }
60- //insert updated row and cells
61- let length = myLibrary . length ;
62- for ( let i = 0 ; i < length ; i ++ ) {
54+
55+ // insert updated rows
56+ for ( let i = 0 ; i < myLibrary . length ; i ++ ) {
6357 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 ( ) {
87- myLibrary [ i ] . check = ! myLibrary [ i ] . check ;
58+
59+ row . insertCell ( 0 ) . innerHTML = myLibrary [ i ] . title ;
60+ row . insertCell ( 1 ) . innerHTML = myLibrary [ i ] . author ;
61+ row . insertCell ( 2 ) . innerHTML = myLibrary [ i ] . pages ;
62+
63+ // read status toggle button
64+ let readCell = row . insertCell ( 3 ) ;
65+ let readButton = document . createElement ( "button" ) ;
66+ readButton . className = "btn btn-success" ;
67+ readButton . innerText = myLibrary [ i ] . read ? "Yes" : "No" ;
68+
69+ readButton . addEventListener ( "click" , function ( ) {
70+ myLibrary [ i ] . read = ! myLibrary [ i ] . read ;
8871 render ( ) ;
8972 } ) ;
9073
91- //add delete button to every row and render again
74+ readCell . appendChild ( readButton ) ;
75+
76+ // delete button
77+ let delCell = row . insertCell ( 4 ) ;
9278 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 } ` ) ;
79+ delButton . className = "btn btn-warning" ;
80+ delButton . innerText = "Delete" ;
81+
82+ delButton . addEventListener ( "click" , function ( ) {
83+ alert ( `You've deleted: ${ myLibrary [ i ] . title } ` ) ;
9984 myLibrary . splice ( i , 1 ) ;
10085 render ( ) ;
10186 } ) ;
87+
88+ delCell . appendChild ( delButton ) ;
10289 }
10390}
0 commit comments