1- let myLibrary = [ ] ;
1+ const myLibrary = [ ] ;
22
33window . addEventListener ( "load" , function ( e ) {
44 populateStorage ( ) ;
@@ -7,39 +7,30 @@ window.addEventListener("load", function (e) {
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+ const book1 = new Book ( "Robison Crusoe" , "Daniel Defoe" , "252" , true ) ;
11+ const 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
23- const title = document . getElementById ( "title" ) ;
24- const author = document . getElementById ( "author" ) ;
25- const pages = document . getElementById ( "pages" ) ;
26- const check = document . getElementById ( "check" ) ;
17+ const titleInput = document . getElementById ( "title" ) ;
18+ const authorInput = document . getElementById ( "author" ) ;
19+ const pagesInput = document . getElementById ( "pages" ) ;
20+ const checkInput = document . getElementById ( "check" ) ;
2721
2822//check the right input from forms and if its ok -> add the new book (object in array)
2923//via Book function and start render function
3024function submit ( ) {
3125 if (
32- author . value == null ||
33- author . value == "" ||
34- title . value == null ||
35- title . value == "" ||
36- pages . value == null ||
37- pages . value == ""
26+ authorInput . value == "" ||
27+ titleInput . value == "" ||
28+ pagesInput . value == ""
3829 ) {
3930 alert ( "Please fill all fields!" ) ;
4031 return false ;
4132 } else {
42- let book = new Book ( title . value , author . value , pages . value , check . checked ) ;
33+ const book = new Book ( titleInput . value , authorInput . value , Number ( pagesInput . value ) , checkInput . checked ) ;
4334 myLibrary . push ( book ) ;
4435 render ( ) ;
4536 }
@@ -53,52 +44,48 @@ function Book(title, author, pages, check) {
5344}
5445
5546function render ( ) {
56- let table = document . getElementById ( "display" ) ;
57- let rowsNumber = table . rows . length ;
47+ const table = document . getElementById ( "display" ) ;
48+ const rowsNumber = table . rows . length ;
5849 //delete old table
5950 for ( let n = rowsNumber - 1 ; n > 0 ; n -- ) {
6051 table . deleteRow ( n ) ;
6152 }
6253 //insert updated row and cells
63- let length = myLibrary . length ;
54+ const length = myLibrary . length ;
6455 for ( let i = 0 ; i < length ; i ++ ) {
65- let row = table . insertRow ( 1 ) ;
66- let titleCell = row . insertCell ( 0 ) ;
67- let authorCell = row . insertCell ( 1 ) ;
68- let pagesCell = row . insertCell ( 2 ) ;
69- let wasReadCell = row . insertCell ( 3 ) ;
70- let deleteCell = row . insertCell ( 4 ) ;
56+ const row = table . insertRow ( 1 ) ;
57+ const titleCell = row . insertCell ( 0 ) ;
58+ const authorCell = row . insertCell ( 1 ) ;
59+ const pagesCell = row . insertCell ( 2 ) ;
60+ const wasReadCell = row . insertCell ( 3 ) ;
61+ const deleteCell = row . insertCell ( 4 ) ;
7162 titleCell . innerHTML = myLibrary [ i ] . title ;
7263 authorCell . innerHTML = myLibrary [ i ] . author ;
7364 pagesCell . innerHTML = myLibrary [ i ] . pages ;
7465
7566 //add and wait for action for read/unread button
76- let changeBut = document . createElement ( "button" ) ;
67+ const changeBut = document . createElement ( "button" ) ;
7768 changeBut . id = i ;
7869 changeBut . className = "btn btn-success" ;
7970 wasReadCell . appendChild ( changeBut ) ;
80- let readStatus = "" ;
81- if ( myLibrary [ i ] . check == false ) {
82- readStatus = "No" ;
83- } else {
84- readStatus = "Yes" ;
85- }
86- changeBut . innerText = readStatus ;
71+
72+ changeBut . innerText = myLibrary [ i ] . check ? "Yes" : "No" ;
8773
8874 changeBut . addEventListener ( "click" , function ( ) {
8975 myLibrary [ i ] . check = ! myLibrary [ i ] . check ;
9076 render ( ) ;
9177 } ) ;
9278
9379 //add delete button to every row and render again
94- let delBut = document . createElement ( "button" ) ;
80+ const delBut = document . createElement ( "button" ) ;
9581 delBut . id = i + 5 ;
9682 deleteCell . appendChild ( delBut ) ;
9783 delBut . className = "btn btn-warning" ;
9884 delBut . innerHTML = "Delete" ;
9985 delBut . addEventListener ( "click" , function ( ) {
100- alert ( `You've deleted title: ${ myLibrary [ i ] . title } ` ) ;
86+ const deletedTitle = myLibrary [ i ] . title ;
10187 myLibrary . splice ( i , 1 ) ;
88+ alert ( `You've deleted title: ${ deletedTitle } ` ) ;
10289 render ( ) ;
10390 } ) ;
10491 }
0 commit comments