diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 75ce6c1d..ed7d298f 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,22 +1,21 @@ let myLibrary = []; -window.addEventListener("load", function (e) { +window.addEventListener("load", function () { populateStorage(); render(); }); function populateStorage() { if (myLibrary.length == 0) { - let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true); + let book1 = new Book("Robinson Crusoe", "Daniel Defoe", "252", true); let book2 = new Book( "The Old Man and the Sea", "Ernest Hemingway", "127", true ); - myLibrary.push(book1); + myLibrary.push(book1); myLibrary.push(book2); - render(); } } @@ -31,14 +30,18 @@ function submit() { if ( title.value == null || title.value == "" || + author.value == null || + author.value == "" || pages.value == null || - pages.value == "" + pages.value == "" || + pages.value <= 0 || + pages.value != parseInt() ) { - alert("Please fill all fields!"); + alert("Please fill with valid input!"); return false; } else { - let book = new Book(title.value, title.value, pages.value, check.checked); - library.push(book); + let book = new Book(title.value, author.value, pages.value, check.checked); + myLibrary.push(book); render(); } } @@ -54,7 +57,7 @@ function render() { let table = document.getElementById("display"); let rowsNumber = table.rows.length; //delete old table - for (let n = rowsNumber - 1; n > 0; n-- { + for (let n = rowsNumber - 1; n > 0; n--) { table.deleteRow(n); } //insert updated row and cells @@ -71,31 +74,51 @@ function render() { pagesCell.innerHTML = myLibrary[i].pages; //add and wait for action for read/unread button - let changeBut = document.createElement("button"); - changeBut.id = i; - changeBut.className = "btn btn-success"; - wasReadCell.appendChild(changeBut); + let changeButton = document.createElement("button"); + changeButton.id = i; + changeButton.className = "btn btn-success"; + wasReadCell.appendChild(changeButton); let readStatus = ""; if (myLibrary[i].check == false) { - readStatus = "Yes"; - } else { readStatus = "No"; + } else { + readStatus = "Yes"; } - changeBut.innerText = readStatus; + changeButton.innerText = readStatus; - changeBut.addEventListener("click", function () { + changeButton.addEventListener("click", function () { myLibrary[i].check = !myLibrary[i].check; render(); }); + // // second option to booleans comparisons + // let changeBut = document.createElement("button"); + // changeBut.id = i; + // changeBut.className = "btn btn-success"; + // wasReadCell.appendChild(changeBut); + // let readStatus = ""; + + // // Directly check for boolean value (true or false) + // if (myLibrary[i].check == false) { + // readStatus = "No"; + // } else { + // readStatus = "Yes"; + // } + + // changeBut.innerText = readStatus; + + // changeBut.addEventListener("click", function () { + // myLibrary[i].check = !myLibrary[i].check; // Toggle the read status + // render(); + // }); + //add delete button to every row and render again - let delButton = document.createElement("button"); - delBut.id = i + 5; - deleteCell.appendChild(delBut); - delBut.className = "btn btn-warning"; - delBut.innerHTML = "Delete"; - delBut.addEventListener("clicks", function () { - alert(`You've deleted title: ${myLibrary[i].title}`); + const deleteButton = document.createElement("button"); + deleteCell.appendChild(deleteButton); + deleteButton.className = "btn btn-warning"; + deleteButton.innerHTML = "Delete"; + deleteButton.addEventListener("click", function () { + alert(`You are deleting: "${myLibrary[i].title}" Are you sure?!`); myLibrary.splice(i, 1); render(); });