diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..2f836120 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "liveServer.settings.port": 5503 +} diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 23acfa71..de831da7 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,12 +1,9 @@ - + - - + My Book Library + + @@ -31,7 +28,7 @@

Library

Library /> add the new book (object in array) //via Book function and start render function function submit() { - if ( - title.value == null || - title.value == "" || - pages.value == null || - pages.value == "" - ) { + const titleValue = titleInput.value.trim(); + const authorValue = authorInput.value.trim(); + const pagesValue = pagesInput.value.trim(); + const pagesNumber = Number(pagesValue); + const isRead = checkInput.checked; + if (!titleValue || !authorValue || !pagesValue) { alert("Please fill all fields!"); return false; - } else { - let book = new Book(title.value, title.value, pages.value, check.checked); - library.push(book); - render(); } + if (!Number.isInteger(pagesNumber) || pagesNumber <= 0) { + alert("Pages can be only whole number greater than 0!"); + return false; + } + + myLibrary.push(new Book(titleValue, authorValue, pagesNumber, isRead)); + + render(); + //clear user input data after storing the information + titleInput.value = ""; + authorInput.value = ""; + pagesInput.value = ""; + checkInput.checked = false; + return true; } function Book(title, author, pages, check) { @@ -54,7 +63,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,30 +80,23 @@ 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 readStatus = ""; - if (myLibrary[i].check == false) { - readStatus = "Yes"; - } else { - readStatus = "No"; - } - changeBut.innerText = readStatus; + let toggleReadButton = document.createElement("button"); + toggleReadButton.className = "btn btn-success"; + wasReadCell.appendChild(toggleReadButton); + let readStatus = myLibrary[i].check ? "Yes" : "No"; + toggleReadButton.innerText = readStatus; - changeBut.addEventListener("click", function () { + toggleReadButton.addEventListener("click", function () { myLibrary[i].check = !myLibrary[i].check; 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 () { + let deleteButton = document.createElement("button"); + deleteCell.appendChild(deleteButton); + deleteButton.className = "btn btn-warning"; + deleteButton.innerHTML = "Delete"; + deleteButton.addEventListener("click", function () { alert(`You've deleted title: ${myLibrary[i].title}`); myLibrary.splice(i, 1); render();