From ee29214ba2093501b41562fc33081bed15c03007 Mon Sep 17 00:00:00 2001 From: joanne342 <130585013+joanne342@users.noreply.github.com> Date: Thu, 30 Jul 2026 08:36:23 +0100 Subject: [PATCH 1/7] Update script.js --- debugging/book-library/script.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 75ce6c1d..ffde8b57 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -32,13 +32,15 @@ function submit() { title.value == null || title.value == "" || pages.value == null || - pages.value == "" + pages.value == "" || + author.value == null || + author.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(); } } @@ -54,7 +56,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 @@ -76,7 +78,7 @@ function render() { changeBut.className = "btn btn-success"; wasReadCell.appendChild(changeBut); let readStatus = ""; - if (myLibrary[i].check == false) { + if (myLibrary[i].check == true) { readStatus = "Yes"; } else { readStatus = "No"; @@ -89,12 +91,12 @@ 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 () { + delBut.addEventListener("click", function () { alert(`You've deleted title: ${myLibrary[i].title}`); myLibrary.splice(i, 1); render(); From 9f6d8d63710ae89500d480fe368376714f4d20cb Mon Sep 17 00:00:00 2001 From: joanne342 <130585013+joanne342@users.noreply.github.com> Date: Thu, 30 Jul 2026 09:37:25 +0100 Subject: [PATCH 2/7] Update script.js --- debugging/book-library/script.js | 141 ++++++++++++++++--------------- 1 file changed, 75 insertions(+), 66 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index ffde8b57..c108878f 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,105 +1,114 @@ -let myLibrary = []; +const myLibrary = []; -window.addEventListener("load", function (e) { +// DOM Elements +const titleInput = document.getElementById("title"); +const authorInput = document.getElementById("author"); +const pagesInput = document.getElementById("pages"); +const checkInput = document.getElementById("check"); + +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( + if (myLibrary.length === 0) { + const book1 = new Book("Robinson Crusoe", "Daniel Defoe", 252, true); + const book2 = new Book( "The Old Man and the Sea", "Ernest Hemingway", - "127", + 127, true ); - myLibrary.push(book1); - myLibrary.push(book2); + + 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"); +function Book(title, author, pages, check) { + this.title = title; + this.author = author; + this.pages = pages; + this.check = 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 == "" || - author.value == null || - author.value == "" - ) { - alert("Please fill all fields!"); + const titleVal = titleInput.value.trim(); + const authorVal = authorInput.value.trim(); + const pagesNum = Number(pagesInput.value); + + if (!titleVal || !authorVal) { + alert("Please enter a valid title and author."); + return false; + } + + if (isNaN(pagesNum) || pagesNum <= 0) { + alert("Please enter a valid page count greater than 0."); return false; - } else { - let book = new Book(title.value, author.value, pages.value, check.checked); - myLibrary.push(book); - render(); } + + const book = new Book(titleVal, authorVal, pagesNum, checkInput.checked); + myLibrary.unshift(book); + + resetForm(); + render(); } -function Book(title, author, pages, check) { - this.title = title; - this.author = author; - this.pages = pages; - this.check = check; +function resetForm() { + titleInput.value = ""; + authorInput.value = ""; + pagesInput.value = ""; + checkInput.checked = false; } function render() { - let table = document.getElementById("display"); - let rowsNumber = table.rows.length; + const table = document.getElementById("display"); + const tbody = table.querySelector("tbody") || table; + //delete old table - for (let n = rowsNumber - 1; n > 0; n--) { - table.deleteRow(n); - } + tbody.innerHTML = ""; + //insert updated row and cells - let length = myLibrary.length; + const 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; + const row = tbody.insertRow(i); + + const titleCell = row.insertCell(0); + const authorCell = row.insertCell(1); + const pagesCell = row.insertCell(2); + const wasReadCell = row.insertCell(3); + const deleteCell = row.insertCell(4); + + 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 == true) { - readStatus = "Yes"; - } else { - readStatus = "No"; - } - changeBut.innerText = readStatus; - - changeBut.addEventListener("click", function () { + const changeBtn = document.createElement("button"); + changeBtn.className = "btn btn-success"; + changeBtn.textContent = myLibrary[i].check ? "Yes" : "No"; + + changeBtn.addEventListener("click", function () { myLibrary[i].check = !myLibrary[i].check; render(); }); + wasReadCell.appendChild(changeBtn); + //add delete button to every row and render again - let delBut = document.createElement("button"); - delBut.id = i + 5; - deleteCell.appendChild(delBut); - delBut.className = "btn btn-warning"; - delBut.innerHTML = "Delete"; - delBut.addEventListener("click", function () { - alert(`You've deleted title: ${myLibrary[i].title}`); + const delBtn = document.createElement("button"); + delBtn.className = "btn btn-warning"; + delBtn.textContent = "Delete"; + + delBtn.addEventListener("click", function () { + const deletedTitle = myLibrary[i].title; myLibrary.splice(i, 1); render(); + alert(`You've deleted title: ${deletedTitle}`); }); + + deleteCell.appendChild(delBtn); } } From d974126c513fe889fd9a017395d99faf1fa53375 Mon Sep 17 00:00:00 2001 From: joanne342 <130585013+joanne342@users.noreply.github.com> Date: Thu, 30 Jul 2026 11:04:32 +0100 Subject: [PATCH 3/7] Update index.html --- debugging/book-library/index.html | 150 +++++++++++++++--------------- 1 file changed, 75 insertions(+), 75 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 23acfa71..57e8bf63 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,20 +1,21 @@ - + - - - - - + Book Library + + + + + + + + + @@ -23,74 +24,73 @@

Library

Add books to your virtual library

- +
+ -
-
- - - - - - -
+
+ + + + + + + + + + + + +
TitleAuthorNumber of PagesReadActions
+
From 635fc89856ae1206046a53e44777ea8d23d1361b Mon Sep 17 00:00:00 2001 From: joanne342 <130585013+joanne342@users.noreply.github.com> Date: Thu, 30 Jul 2026 12:39:56 +0100 Subject: [PATCH 4/7] Update style.css --- debugging/book-library/style.css | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/debugging/book-library/style.css b/debugging/book-library/style.css index 302950cb..45723f37 100644 --- a/debugging/book-library/style.css +++ b/debugging/book-library/style.css @@ -5,7 +5,7 @@ padding-left: 20px; } -.btn { +#demo .btn { display: block; } From 3d39522aa9a04a2c08c19c1ab5a28e59b92f2354 Mon Sep 17 00:00:00 2001 From: joanne342 <130585013+joanne342@users.noreply.github.com> Date: Thu, 30 Jul 2026 12:40:21 +0100 Subject: [PATCH 5/7] Update index.html --- debugging/book-library/index.html | 142 ++++++++++++++---------------- 1 file changed, 67 insertions(+), 75 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 57e8bf63..cfa6366c 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,21 +1,20 @@ - + - Book Library - - - + Book library + + + + - - - - - - @@ -24,73 +23,66 @@

Library

Add books to your virtual library

-
- + -
-
- +
+
+ + + + + + +
+ type="checkbox" + class="form-check-input" + id="check" + value="" + />Read + +
- - - - - - - - - - - - -
TitleAuthorNumber of PagesReadActions
+ + + + + + + + + + + + +
TitleAuthorNumber of PagesRead
+ + From bf9c2e8b813256daefb0c4598655b4e5331e9e52 Mon Sep 17 00:00:00 2001 From: joanne342 <130585013+joanne342@users.noreply.github.com> Date: Thu, 30 Jul 2026 12:40:47 +0100 Subject: [PATCH 6/7] Update script.js --- debugging/book-library/script.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index c108878f..13e92e7c 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -6,6 +6,8 @@ const authorInput = document.getElementById("author"); const pagesInput = document.getElementById("pages"); const checkInput = document.getElementById("check"); +const submitBtn = document.getElementById("submit-btn"); + window.addEventListener("load", function () { populateStorage(); }); @@ -56,6 +58,8 @@ function submit() { render(); } +submitBtn.addEventListener("click", submit); + function resetForm() { titleInput.value = ""; authorInput.value = ""; From cd24fbe058e2305b61ad01eceab808b24fe81aae Mon Sep 17 00:00:00 2001 From: joanne342 <130585013+joanne342@users.noreply.github.com> Date: Thu, 30 Jul 2026 12:42:26 +0100 Subject: [PATCH 7/7] Update script.js --- debugging/book-library/script.js | 1 - 1 file changed, 1 deletion(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 13e92e7c..19ae3fb4 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -5,7 +5,6 @@ const titleInput = document.getElementById("title"); const authorInput = document.getElementById("author"); const pagesInput = document.getElementById("pages"); const checkInput = document.getElementById("check"); - const submitBtn = document.getElementById("submit-btn"); window.addEventListener("load", function () {