diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 23acfa71..7f639a57 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 - /> + > + >
@@ -91,6 +88,6 @@

Library

- + diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 75ce6c1d..9259e9fa 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -2,7 +2,6 @@ let myLibrary = []; window.addEventListener("load", function (e) { populateStorage(); - render(); }); function populateStorage() { @@ -20,29 +19,51 @@ function populateStorage() { } } -const title = document.getElementById("title"); -const author = document.getElementById("author"); -const pages = document.getElementById("pages"); -const check = document.getElementById("check"); +const titleDom = document.getElementById("title"); +const authorDom = document.getElementById("author"); +const pagesDom = document.getElementById("pages"); +const checkDom = document.getElementById("check"); + +document.getElementById('submit-btn').addEventListener('click', submit); //check the right input from forms and if its ok -> add the new book (object in array) //via Book function and start render function function submit() { + const title = sanitize(titleDom.value.trim()); + const author = sanitize(authorDom.value.trim()); + let pages = sanitize(pagesDom.value.trim()); + if (isNaN(pages)){ + alert("Enter valid value for page number!"); + return false; + } + pages = parseInt(pages); + if (!isValueInteger(pages)){ + alert("Enter valid value for page number!"); + return false; + } + if ( - title.value == null || - title.value == "" || - pages.value == null || - pages.value == "" + title == "" || + author == "" || + pages < 0 ) { - alert("Please fill all fields!"); + alert("Please fill all fields or enter valid values!"); return false; } else { - let book = new Book(title.value, title.value, pages.value, check.checked); - library.push(book); + let book = new Book(title, author, pages, checkDom.checked); + myLibrary.push(book); render(); } } +function isValueInteger(value) { + return Number.isInteger(value); +} + +function sanitize(string) { + return string.replace(/[^\w\s]/gi, ''); +} + function Book(title, author, pages, check) { this.title = title; this.author = author; @@ -52,36 +73,29 @@ 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); - } + let tableBody = table.querySelector("tbody"); + tableBody.textContent = ''; //insert updated row and cells let length = myLibrary.length; for (let i = 0; i < length; i++) { - let row = table.insertRow(1); + let row = tableBody.insertRow(0); 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"; - } - changeBut.innerText = readStatus; + let readStatus = myLibrary[i].check ? "Yes" : "No"; + changeBut.textContent = readStatus; changeBut.addEventListener("click", function () { myLibrary[i].check = !myLibrary[i].check; @@ -89,14 +103,14 @@ function render() { }); //add delete button to every row and render again - let delButton = document.createElement("button"); + let delBut = 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}`); + delBut.textContent = "Delete"; + delBut.addEventListener("click", function () { myLibrary.splice(i, 1); + alert(`You've deleted title: ${myLibrary[i].title}`); render(); }); } diff --git a/debugging/book-library/style.css b/debugging/book-library/style.css index 302950cb..2d40464b 100644 --- a/debugging/book-library/style.css +++ b/debugging/book-library/style.css @@ -1,7 +1,7 @@ .form-group { width: 400px; height: 300px; - align-self: left; + align-self: flex-start; padding-left: 20px; }