diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 23acfa71..50ab3cd3 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,20 +1,17 @@ - + - - + Book Library + + - + > + @@ -31,20 +28,20 @@

Library

+ > + > Library id="pages" name="pages" required - /> + > + onclick="submit();" + >
@@ -80,7 +77,7 @@

Library

- + diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 75ce6c1d..9d7aa8e2 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -6,7 +6,7 @@ window.addEventListener("load", function (e) { }); function populateStorage() { - if (myLibrary.length == 0) { + if (myLibrary.length === 0) { let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true); let book2 = new Book( "The Old Man and the Sea", @@ -29,16 +29,17 @@ const check = document.getElementById("check"); //via Book function and start render function function submit() { if ( - title.value == null || - title.value == "" || - pages.value == null || - pages.value == "" + title.value.trim() === "" || + author.value.trim() === "" || + pages.value.trim() === "" || + pages.value < 1 || + Number.isInteger(pages.value) ) { - alert("Please fill all fields!"); + alert("Please use proper inputs and fill all fields!"); 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(); } } @@ -51,35 +52,32 @@ function Book(title, author, pages, check) { } function render() { - let table = document.getElementById("display"); - let rowsNumber = table.rows.length; //delete old table - for (let n = rowsNumber - 1; n > 0; n-- { - table.deleteRow(n); - } + + document.getElementById("table-body").innerHTML = ` ` + //insert updated row and cells let length = myLibrary.length; for (let i = 0; i < length; i++) { - let row = table.insertRow(1); + let row = document.getElementById("table-body").insertRow(); let titleCell = row.insertCell(0); let authorCell = row.insertCell(1); let pagesCell = row.insertCell(2); let wasReadCell = row.insertCell(3); let deleteCell = row.insertCell(4); - titleCell.innerHTML = myLibrary[i].title; - authorCell.innerHTML = myLibrary[i].author; - pagesCell.innerHTML = myLibrary[i].pages; + titleCell.textContent = myLibrary[i].title; + authorCell.textContent = myLibrary[i].author; + pagesCell.textContent = 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 readStatus = ""; if (myLibrary[i].check == false) { - readStatus = "Yes"; - } else { readStatus = "No"; + } else { + readStatus = "Yes"; } changeBut.innerText = readStatus; @@ -89,12 +87,11 @@ function render() { }); //add delete button to every row and render again - let delButton = document.createElement("button"); - delBut.id = i + 5; + let delBut = document.createElement("button"); deleteCell.appendChild(delBut); delBut.className = "btn btn-warning"; delBut.innerHTML = "Delete"; - delBut.addEventListener("clicks", function () { + delBut.addEventListener("click", function () { alert(`You've deleted title: ${myLibrary[i].title}`); myLibrary.splice(i, 1); render();