diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 23acfa71..a8ba76bb 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,20 +1,13 @@ - + - - + Book Library + - - + + @@ -31,42 +24,35 @@

Library

+ required> + required> + required> + onclick="submitBook(event);">
diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 75ce6c1d..38766f02 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,46 +1,61 @@ 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 book2 = new Book( - "The Old Man and the Sea", - "Ernest Hemingway", - "127", - true - ); - myLibrary.push(book1); - myLibrary.push(book2); - render(); + if (myLibrary.length === 0) { + const book1 = new Book("Robison Crusoe", "Daniel Defoe", 252, true); + const book2 = new Book("The Old Man and the Sea", "Ernest Hemingway", 127, true); + myLibrary.push(book1, book2); } + render(); } -const title = document.getElementById("title"); -const author = document.getElementById("author"); -const pages = document.getElementById("pages"); -const check = document.getElementById("check"); - -//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() { - if ( - title.value == null || - title.value == "" || - pages.value == null || - pages.value == "" - ) { - alert("Please fill all fields!"); - return false; - } else { - let book = new Book(title.value, title.value, pages.value, check.checked); - library.push(book); - render(); +const titleEl = document.getElementById("title"); +const authorEl = document.getElementById("author"); +const pagesEl = document.getElementById("pages"); +const checkEl = document.getElementById("check"); + +// Add book from form +function submitBook(event) { + // Prevent page reload + event.preventDefault(); + + const titleVal = titleEl.value.trim(); + const authorVal = authorEl.value.trim(); + const pagesRaw = pagesEl.value.trim(); + + // Check if pages input is a positive whole number + if (!/^\d+$/.test(pagesRaw)) { + alert("Please enter a valid positive whole number for pages!"); + return; + } + + const pagesVal = Number(pagesRaw); + + if (!titleVal || !authorVal) { + alert("Please fill all fields!"); + return; + } + + if (pagesVal <= 0) { + alert("Please enter a realistic positive number of pages!"); + return; } + + // Add book to library + + const book = new Book(titleVal, authorVal, pagesVal, checkEl.checked); + myLibrary.push(book); + render(); + + // Clear form + titleEl.value = ""; + authorEl.value = ""; + pagesEl.value = ""; + checkEl.checked = false; } function Book(title, author, pages, check) { @@ -51,53 +66,56 @@ 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); - } - //insert updated row and cells - let length = myLibrary.length; - for (let i = 0; i < length; i++) { - let row = table.insertRow(1); - 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; - - //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"; + const table = document.getElementById("display"); + const tbody = table.querySelector("tbody"); + + // Clear existing rows + tbody.innerHTML = ""; + + myLibrary.forEach((book, i) => { + const row = tbody.insertRow(); + + // keep all cells in one place + const titleCell = row.insertCell(0); + const authorCell = row.insertCell(1); + const pagesCell = row.insertCell(2); + const checkCell = row.insertCell(3); + const delCell = row.insertCell(4); // delete button cell + + titleCell.textContent = book.title; + authorCell.textContent = book.author; + pagesCell.textContent = book.pages; + + // use helper function for read button + createReadButton(checkCell, i); + + // use helper function for delete button + createDeleteCell(delCell, i); + }); +} + +function createDeleteCell(cell, index) { + const deleteBtn = document.createElement("button"); + deleteBtn.textContent = "Delete"; + deleteBtn.addEventListener("click", () => { + if (index > -1){ + myLibrary.splice(index, 1); + render(); } - changeBut.innerText = readStatus; - - changeBut.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 () { - alert(`You've deleted title: ${myLibrary[i].title}`); - myLibrary.splice(i, 1); - render(); - }); - } + }); + cell.appendChild(deleteBtn); } + +// helper function for read button +function createReadButton(cell, index) { + const readBtn = document.createElement("button"); + readBtn.textContent = myLibrary[index].check ? "Read" : "Not Read"; + readBtn.className = myLibrary[index].check ? "btn btn-success" : "btn btn-secondary"; + + readBtn.addEventListener("click", () => { + myLibrary[index].check = !myLibrary[index].check; + render(); + }); + + cell.appendChild(readBtn); +} \ No newline at end of file