From d9bdadacac648833893a1039325b94e404e784f0 Mon Sep 17 00:00:00 2001 From: FIRST_NAME LAST_NAME Date: Thu, 27 Mar 2025 01:50:49 +0000 Subject: [PATCH 1/5] problem solution --- debugging/book-library/index.html | 72 +++++---------------- debugging/book-library/script.js | 100 ++++++++++++------------------ debugging/book-library/style.css | 10 ++- 3 files changed, 66 insertions(+), 116 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 23acfa7..10b98fd 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,19 +1,13 @@ - + - - + My Book Library + + - + @@ -23,50 +17,24 @@

Library

Add books to your virtual library

- +
- - - + + + + + - + + - + +
@@ -81,13 +49,7 @@

Library

- - - - - - - + diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 75ce6c1..7a91714 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,23 +1,18 @@ 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 - ); + if (myLibrary.length === 0) { + 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(book2); - render(); } + render(); } const title = document.getElementById("title"); @@ -25,21 +20,15 @@ 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 == "" - ) { + if (!title.value || !author.value || !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); + let book = new Book(title.value, author.value, pages.value, check.checked); + myLibrary.push(book); render(); + clearForm(); } } @@ -53,51 +42,44 @@ 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-- { + + + 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"; - } - changeBut.innerText = readStatus; - - changeBut.addEventListener("click", function () { - myLibrary[i].check = !myLibrary[i].check; + + myLibrary.forEach((book, i) => { + let row = table.insertRow(); + row.insertCell(0).innerText = book.title; + row.insertCell(1).innerText = book.author; + row.insertCell(2).innerText = book.pages; + + + let readButton = document.createElement("button"); + readButton.className = "btn btn-success"; + readButton.innerText = book.check ? "Yes" : "No"; + readButton.addEventListener("click", () => { + book.check = !book.check; render(); }); - - //add delete button to every row and render again + row.insertCell(3).appendChild(readButton); + + 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}`); + delButton.className = "btn btn-warning"; + delButton.innerText = "Delete"; + delButton.addEventListener("click", () => { + alert(`You've deleted title: ${book.title}`); myLibrary.splice(i, 1); render(); }); - } + row.insertCell(4).appendChild(delButton); + }); +} + +function clearForm() { + title.value = ""; + author.value = ""; + pages.value = ""; + check.checked = false; } diff --git a/debugging/book-library/style.css b/debugging/book-library/style.css index 302950c..f01187f 100644 --- a/debugging/book-library/style.css +++ b/debugging/book-library/style.css @@ -1,19 +1,25 @@ .form-group { width: 400px; height: 300px; - align-self: left; + text-align: left; padding-left: 20px; } .btn { display: block; + width: 150px; + margin-bottom: 10px; } .form-check-label { padding-left: 20px; - margin: 5px 0px 5px 0px; + margin: 5px 0; } button.btn-info { margin: 20px; } + +.table button { + margin-right: 5px; +} From e63fd038681db38131397e6e2188d7b6e5012393 Mon Sep 17 00:00:00 2001 From: FIRST_NAME LAST_NAME Date: Sun, 6 Apr 2025 03:45:28 +0100 Subject: [PATCH 2/5] doing chages --- debugging/book-library/script.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 7a91714..555f398 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -7,8 +7,8 @@ window.addEventListener("load", function () { function populateStorage() { if (myLibrary.length === 0) { - let book1 = new Book("Robinson Crusoe", "Daniel Defoe", "252", true); - let book2 = new Book("The Old Man and the Sea", "Ernest Hemingway", "127", true); + const book1 = new Book("Robinson Crusoe", "Daniel Defoe", "252", true); + const book2 = new Book("The Old Man and the Sea", "Ernest Hemingway", "127", true); myLibrary.push(book1); myLibrary.push(book2); } @@ -40,7 +40,7 @@ function Book(title, author, pages, check) { } function render() { - let table = document.getElementById("display"); + const table = document.getElementById("display"); let rowsNumber = table.rows.length; @@ -55,7 +55,7 @@ function render() { row.insertCell(2).innerText = book.pages; - let readButton = document.createElement("button"); + const readButton = document.createElement("button"); readButton.className = "btn btn-success"; readButton.innerText = book.check ? "Yes" : "No"; readButton.addEventListener("click", () => { @@ -65,7 +65,7 @@ function render() { row.insertCell(3).appendChild(readButton); - let delButton = document.createElement("button"); + const delButton = document.createElement("button"); delButton.className = "btn btn-warning"; delButton.innerText = "Delete"; delButton.addEventListener("click", () => { From 9c3f9d7bebebc3df63ebe72458f6518a1e3d9b40 Mon Sep 17 00:00:00 2001 From: FIRST_NAME LAST_NAME Date: Mon, 7 Apr 2025 14:17:15 +0100 Subject: [PATCH 3/5] new changed --- debugging/book-library/script.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 555f398..095ff35 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -25,7 +25,7 @@ function submit() { alert("Please fill all fields!"); return false; } else { - let book = new Book(title.value, author.value, pages.value, check.checked); + const book = new Book(title.value, author.value, pages.value, check.checked); myLibrary.push(book); render(); clearForm(); @@ -49,7 +49,7 @@ function render() { } myLibrary.forEach((book, i) => { - let row = table.insertRow(); + const row = table.insertRow(); row.insertCell(0).innerText = book.title; row.insertCell(1).innerText = book.author; row.insertCell(2).innerText = book.pages; From 191133be82bc72d85d2cc052b1e1e8e15cd5c5cb Mon Sep 17 00:00:00 2001 From: FIRST_NAME LAST_NAME Date: Mon, 7 Apr 2025 23:13:49 +0100 Subject: [PATCH 4/5] last changhed --- debugging/book-library/script.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 095ff35..23452d2 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -41,7 +41,7 @@ function Book(title, author, pages, check) { function render() { const table = document.getElementById("display"); - let rowsNumber = table.rows.length; + const rowsNumber = table.rows.length; for (let n = rowsNumber - 1; n > 0; n--) { From e2b3c57d4e542046174b575b1b5df838b8b233e4 Mon Sep 17 00:00:00 2001 From: FIRST_NAME LAST_NAME Date: Wed, 9 Apr 2025 23:45:34 +0100 Subject: [PATCH 5/5] problem solving --- debugging/book-library/script.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 23452d2..e522f47 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,4 +1,4 @@ -let myLibrary = []; +const myLibrary = []; window.addEventListener("load", function () { populateStorage();