From e98b0a89abbcd173c9d57429cd52bc585f190b6b Mon Sep 17 00:00:00 2001 From: HannaOdud Date: Mon, 8 Dec 2025 16:08:23 +0000 Subject: [PATCH 1/7] Delete button fixed --- debugging/book-library/script.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 75ce6c1d..15330408 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -54,7 +54,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 @@ -89,12 +89,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 a22397426a9d128615d709cc8ab900b13720caed Mon Sep 17 00:00:00 2001 From: HannaOdud Date: Mon, 8 Dec 2025 16:14:25 +0000 Subject: [PATCH 2/7] Add book fixed --- 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 15330408..338044f2 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -38,7 +38,7 @@ function submit() { return false; } else { let book = new Book(title.value, title.value, pages.value, check.checked); - library.push(book); + myLibrary.push(book); render(); } } From ad1b8ffa856acd3b1bb0775db8f6bf89f4334858 Mon Sep 17 00:00:00 2001 From: HannaOdud Date: Mon, 8 Dec 2025 17:08:15 +0000 Subject: [PATCH 3/7] button wasRead fixed --- 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 338044f2..92e8c8d1 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -77,9 +77,9 @@ function render() { wasReadCell.appendChild(changeBut); let readStatus = ""; if (myLibrary[i].check == false) { - readStatus = "Yes"; - } else { readStatus = "No"; + } else { + readStatus = "Yes"; } changeBut.innerText = readStatus; From e9dbd8de099d90552be97c56dcd84f28d5ecdb2c Mon Sep 17 00:00:00 2001 From: HannaOdud Date: Mon, 8 Dec 2025 17:11:47 +0000 Subject: [PATCH 4/7] author/title name fixed --- 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 92e8c8d1..2d925ec0 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -37,7 +37,7 @@ function submit() { alert("Please fill all fields!"); return false; } else { - let book = new Book(title.value, title.value, pages.value, check.checked); + let book = new Book(title.value, author.value, pages.value, check.checked); myLibrary.push(book); render(); } From 788b19bc019430529a45653ee1c31a5257ad1623 Mon Sep 17 00:00:00 2001 From: HannaOdud Date: Wed, 10 Dec 2025 23:01:25 +0000 Subject: [PATCH 5/7] more fixes --- debugging/book-library/index.html | 35 +++++++++++++--------------- debugging/book-library/script.js | 38 +++++++++++++++---------------- debugging/book-library/style.css | 2 +- 3 files changed, 35 insertions(+), 40 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 23acfa71..9e8c8f5b 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,20 +1,17 @@ - + - - + Book Library + + - + > + @@ -31,42 +28,42 @@

Library

+ > + > + > + >
@@ -91,6 +88,6 @@

Library

- + diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 2d925ec0..dc04bbd6 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,24 +19,25 @@ 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() { if ( - title.value == null || - title.value == "" || - pages.value == null || - pages.value == "" + titleDom.value == "" || + authorDom.value == "" || + pagesDom.value == 0 ) { alert("Please fill all fields!"); return false; } else { - let book = new Book(title.value, author.value, pages.value, check.checked); + let book = new Book(titleDom.value.trim(), authorDom.value.trim(), pagesDom.value, checkDom.checked); myLibrary.push(book); render(); } @@ -52,11 +52,9 @@ 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++) { @@ -66,9 +64,9 @@ function render() { 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"); @@ -81,7 +79,7 @@ function render() { } else { readStatus = "Yes"; } - changeBut.innerText = readStatus; + changeBut.textContent = readStatus; changeBut.addEventListener("click", function () { myLibrary[i].check = !myLibrary[i].check; @@ -93,10 +91,10 @@ function render() { delBut.id = i + 5; deleteCell.appendChild(delBut); delBut.className = "btn btn-warning"; - delBut.innerHTML = "Delete"; + delBut.textContent = "Delete"; delBut.addEventListener("click", function () { - alert(`You've deleted title: ${myLibrary[i].title}`); 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; } From d1e76419fee519204ea9d57433f0768649c7e296 Mon Sep 17 00:00:00 2001 From: HannaOdud Date: Mon, 15 Dec 2025 13:55:04 +0000 Subject: [PATCH 6/7] Submit function refactoring --- debugging/book-library/index.html | 2 +- debugging/book-library/script.js | 26 +++++++++++++++++++++----- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 9e8c8f5b..7f639a57 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -44,7 +44,7 @@

Library

> 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()); + const pages = sanitize(pagesDom.value.trim()); if ( - titleDom.value == "" || - authorDom.value == "" || - pagesDom.value == 0 + title == "" || + author == "" || + pages == "" || + parseInt(pages) < 0 ) { - alert("Please fill all fields!"); + alert("Please fill all fields or enter valid values!"); return false; } else { - let book = new Book(titleDom.value.trim(), authorDom.value.trim(), pagesDom.value, checkDom.checked); + let book = new Book(title, author, pages, checkDom.checked); myLibrary.push(book); render(); } } +function sanitize(string) { + const map = { + '&': '&', + '<': '<', + '>': '>', + '"': '"', + "'": ''', + "/": '/', + }; + const reg = /[&<>"'/]/ig; + return string.replace(reg, (match)=>(map[match])); +} function Book(title, author, pages, check) { this.title = title; From 1c78bde5a2934a74b8a78bfd0f40366c6a442439 Mon Sep 17 00:00:00 2001 From: HannaOdud Date: Tue, 16 Dec 2025 14:25:31 +0000 Subject: [PATCH 7/7] refactowing and bugfixing --- debugging/book-library/script.js | 40 ++++++++++++++++---------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 8327e445..9259e9fa 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -31,12 +31,21 @@ document.getElementById('submit-btn').addEventListener('click', submit); function submit() { const title = sanitize(titleDom.value.trim()); const author = sanitize(authorDom.value.trim()); - const pages = sanitize(pagesDom.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 == "" || author == "" || - pages == "" || - parseInt(pages) < 0 + pages < 0 ) { alert("Please fill all fields or enter valid values!"); return false; @@ -46,17 +55,13 @@ function submit() { render(); } } + +function isValueInteger(value) { + return Number.isInteger(value); +} + function sanitize(string) { - const map = { - '&': '&', - '<': '<', - '>': '>', - '"': '"', - "'": ''', - "/": '/', - }; - const reg = /[&<>"'/]/ig; - return string.replace(reg, (match)=>(map[match])); + return string.replace(/[^\w\s]/gi, ''); } function Book(title, author, pages, check) { @@ -74,7 +79,7 @@ function render() { //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); @@ -89,12 +94,7 @@ function render() { changeBut.id = i; changeBut.className = "btn btn-success"; wasReadCell.appendChild(changeBut); - let readStatus = ""; - if (myLibrary[i].check == false) { - readStatus = "No"; - } else { - readStatus = "Yes"; - } + let readStatus = myLibrary[i].check ? "Yes" : "No"; changeBut.textContent = readStatus; changeBut.addEventListener("click", function () {