@@ -7,7 +7,7 @@ window.addEventListener("load", function (e) {
77
88function populateStorage ( ) {
99 if ( myLibrary . length == 0 ) {
10- let book1 = new Book ( "Robison Crusoe" , "Daniel Defoe" , "252" , true ) ;
10+ let book1 = new Book ( "Robinson Crusoe" , "Daniel Defoe" , "252" , true ) ; // Corrected book title
1111 let book2 = new Book (
1212 "The Old Man and the Sea" ,
1313 "Ernest Hemingway" ,
@@ -25,20 +25,20 @@ const author = document.getElementById("author");
2525const pages = document . getElementById ( "pages" ) ;
2626const check = document . getElementById ( "check" ) ;
2727
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
3028function submit ( ) {
3129 if (
3230 title . value == null ||
3331 title . value == "" ||
32+ author . value == null || // Check for author input
33+ author . value == "" ||
3434 pages . value == null ||
3535 pages . value == ""
3636 ) {
3737 alert ( "Please fill all fields!" ) ;
3838 return false ;
3939 } else {
40- let book = new Book ( title . value , title . value , pages . value , check . checked ) ;
41- library . push ( book ) ;
40+ let book = new Book ( title . value , author . value , pages . value , check . checked ) ; // Passed author.value instead of title.value
41+ myLibrary . push ( book ) ;
4242 render ( ) ;
4343 }
4444}
@@ -53,11 +53,11 @@ function Book(title, author, pages, check) {
5353function render ( ) {
5454 let table = document . getElementById ( "display" ) ;
5555 let rowsNumber = table . rows . length ;
56- //delete old table
57- for ( let n = rowsNumber - 1 ; n > 0 ; n -- {
56+ // Delete old table rows
57+ for ( let n = rowsNumber - 1 ; n > 0 ; n -- ) { // Corrected the loop condition
5858 table . deleteRow ( n ) ;
5959 }
60- //insert updated row and cells
60+ // Insert updated rows and cells
6161 let length = myLibrary . length ;
6262 for ( let i = 0 ; i < length ; i ++ ) {
6363 let row = table . insertRow ( 1 ) ;
@@ -70,31 +70,26 @@ function render() {
7070 cell2 . innerHTML = myLibrary [ i ] . author ;
7171 cell3 . innerHTML = myLibrary [ i ] . pages ;
7272
73- //add and wait for action for read/unread button
73+ // Add read/unread button
7474 let changeBut = document . createElement ( "button" ) ;
7575 changeBut . id = i ;
7676 changeBut . className = "btn btn-success" ;
7777 cell4 . appendChild ( changeBut ) ;
78- let readStatus = "" ;
79- if ( myLibrary [ i ] . check == false ) {
80- readStatus = "Yes" ;
81- } else {
82- readStatus = "No" ;
83- }
78+ let readStatus = myLibrary [ i ] . check ? "No" : "Yes" ; // Corrected readStatus assignment
8479 changeBut . innerHTML = readStatus ;
8580
8681 changeBut . addEventListener ( "click" , function ( ) {
8782 myLibrary [ i ] . check = ! myLibrary [ i ] . check ;
8883 render ( ) ;
8984 } ) ;
9085
91- //add delete button to every row and render again
86+ // Add delete button
9287 let delButton = document . createElement ( "button" ) ;
93- delBut . id = i + 5 ;
94- cell5 . appendChild ( delBut ) ;
95- delBut . className = "btn btn-warning" ;
96- delBut . innerHTML = "Delete" ;
97- delBut . addEventListener ( "clicks " , function ( ) {
88+ delButton . id = i + 5 ; // Corrected variable name from delBut to delButton
89+ cell5 . appendChild ( delButton ) ;
90+ delButton . className = "btn btn-warning" ;
91+ delButton . innerHTML = "Delete" ;
92+ delButton . addEventListener ( "click " , function ( ) { // Corrected event name from "clicks" to "click"
9893 alert ( `You've deleted title: ${ myLibrary [ i ] . title } ` ) ;
9994 myLibrary . splice ( i , 1 ) ;
10095 render ( ) ;
0 commit comments