From 896cf58ac49bc4a434ccec09aa861f0a99383683 Mon Sep 17 00:00:00 2001 From: TarasMykytiuk Date: Sun, 17 Aug 2025 20:42:37 +0100 Subject: [PATCH 1/6] first commit --- 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 75ce6c1d..abcd4293 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,7 +89,7 @@ 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"; From 7b319e72e048b628b4107b063999ea543deb520d Mon Sep 17 00:00:00 2001 From: TarasMykytiuk Date: Sun, 17 Aug 2025 20:53:16 +0100 Subject: [PATCH 2/6] bugfixing --- debugging/book-library/script.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index abcd4293..0ae3cdad 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -31,14 +31,16 @@ function submit() { if ( title.value == null || title.value == "" || + author.value == null || + author.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); + let book = new Book(title.value, author.value, pages.value, check.checked); + myLibrary.push(book); render(); } } @@ -77,9 +79,9 @@ function render() { wasReadCell.appendChild(changeBut); let readStatus = ""; if (myLibrary[i].check == false) { - readStatus = "Yes"; - } else { readStatus = "No"; + } else { + readStatus = "Yes"; } changeBut.innerText = readStatus; @@ -94,7 +96,7 @@ function render() { 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 ff05f0f69590432234ce8c56ab155b3bdcf784e5 Mon Sep 17 00:00:00 2001 From: TarasMykytiuk Date: Mon, 18 Aug 2025 20:45:48 +0100 Subject: [PATCH 3/6] index.html fixed --- debugging/book-library/index.html | 33 ++++++++++++++----------------- 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 23acfa71..50757c73 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,20 +1,17 @@ - + - - + Library + + - + > + @@ -31,42 +28,42 @@

Library

+ > + > + > + >
@@ -93,4 +90,4 @@

Library

- + \ No newline at end of file From 6dcafb4394a462a7f983d779d4a561691b715415 Mon Sep 17 00:00:00 2001 From: TarasMykytiuk Date: Mon, 18 Aug 2025 21:51:57 +0100 Subject: [PATCH 4/6] script.js fixed according to feedback.md --- debugging/book-library/script.js | 92 +++++++++++++++++++------------- 1 file changed, 55 insertions(+), 37 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 0ae3cdad..44729826 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -2,16 +2,15 @@ let myLibrary = []; window.addEventListener("load", function (e) { populateStorage(); - render(); }); function populateStorage() { if (myLibrary.length == 0) { - let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true); + let book1 = new Book("Robison Crusoe", "Daniel Defoe", Number("252"), true); let book2 = new Book( "The Old Man and the Sea", "Ernest Hemingway", - "127", + parseInt("127"), true ); myLibrary.push(book1); @@ -20,26 +19,50 @@ function populateStorage() { } } -const title = document.getElementById("title"); -const author = document.getElementById("author"); -const pages = document.getElementById("pages"); -const check = document.getElementById("check"); +const bookTitleInput = document.getElementById("title"); +const bookAuthorInput = document.getElementById("author"); +const bookNumberOfPagesInput = document.getElementById("pages"); +const isBookReadCheckBox = document.getElementById("check"); + +// Strip out HTML tags (to prevent XSS) +function sanitizeInput(str) { + const div = document.createElement('div'); + div.textContent = str; + return div.innerHTML; +} + +// To check if pages input can be safely converted to integer +function isValidInteger(input) { + return Number.isInteger(input); +} +// prevent accidental hexadecimal input +function isHexadecimal(input) { + return /^0x[0-9a-fA-F]+$/.test(input); +} //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 == "" || - author.value == null || - author.value == "" || - pages.value == null || - pages.value == "" + bookTitleInput.value.trim() == "" || + bookAuthorInput.value.trim() == "" || + bookNumberOfPagesInput.value.trim() == "" ) { alert("Please fill all fields!"); return false; - } else { - let book = new Book(title.value, author.value, pages.value, check.checked); + } else if ( + isNaN(Number(bookNumberOfPagesInput.value.trim())) || + !isValidInteger(Number(bookNumberOfPagesInput.value.trim())) || + isHexadecimal(bookNumberOfPagesInput.value.trim()) || + Number(bookNumberOfPagesInput.value.trim()) <= 0 + ) { + alert("Invalid number of pages format!"); + return false; + } else { + const bookTitle = sanitizeInput(bookTitleInput.value.trim()); + const bookAuthor = sanitizeInput(bookAuthorInput.value.trim()); + const bookNumberOfPages = parseInt(sanitizeInput(bookNumberOfPagesInput.value.trim()), 10); + let book = new Book(bookTitle, bookAuthor, bookNumberOfPages, isBookReadCheckBox.checked); myLibrary.push(book); render(); } @@ -54,51 +77,46 @@ 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.innerHTML = ''; //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(); 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; + 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 changeReadStatusButton = document.createElement("button"); + changeReadStatusButton.className = "btn btn-success"; + wasReadCell.appendChild(changeReadStatusButton); let readStatus = ""; if (myLibrary[i].check == false) { readStatus = "No"; } else { readStatus = "Yes"; } - changeBut.innerText = readStatus; + changeReadStatusButton.innerText = readStatus; - changeBut.addEventListener("click", function () { + changeReadStatusButton.addEventListener("click", function () { myLibrary[i].check = !myLibrary[i].check; render(); }); //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}`); + let deleteBookButton = document.createElement("button"); + deleteCell.appendChild(deleteBookButton); + deleteBookButton.className = "btn btn-warning"; + deleteBookButton.innerHTML = "Delete"; + deleteBookButton.addEventListener("click", function () { myLibrary.splice(i, 1); + alert(`You've deleted title: ${myLibrary[i].title}`); render(); }); } From bafe9668fd5bdbc3cc1ce4c9c1eb6c8c75cb43a4 Mon Sep 17 00:00:00 2001 From: TarasMykytiuk Date: Tue, 19 Aug 2025 12:32:16 +0100 Subject: [PATCH 5/6] reaction to feedback --- debugging/book-library/script.js | 41 +++++++++++--------------------- 1 file changed, 14 insertions(+), 27 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 44729826..e1556f48 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -6,11 +6,11 @@ window.addEventListener("load", function (e) { function populateStorage() { if (myLibrary.length == 0) { - let book1 = new Book("Robison Crusoe", "Daniel Defoe", Number("252"), true); + let book1 = new Book("Robison Crusoe", "Daniel Defoe", 252, true); let book2 = new Book( "The Old Man and the Sea", "Ernest Hemingway", - parseInt("127"), + 127, true ); myLibrary.push(book1); @@ -24,13 +24,6 @@ const bookAuthorInput = document.getElementById("author"); const bookNumberOfPagesInput = document.getElementById("pages"); const isBookReadCheckBox = document.getElementById("check"); -// Strip out HTML tags (to prevent XSS) -function sanitizeInput(str) { - const div = document.createElement('div'); - div.textContent = str; - return div.innerHTML; -} - // To check if pages input can be safely converted to integer function isValidInteger(input) { return Number.isInteger(input); @@ -43,26 +36,24 @@ function isHexadecimal(input) { //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 ( - bookTitleInput.value.trim() == "" || - bookAuthorInput.value.trim() == "" || - bookNumberOfPagesInput.value.trim() == "" - ) { + const bookTitle = bookTitleInput.value.trim(); + const bookAuthor = bookAuthorInput.value.trim(); + const bookNumberOfPages = bookNumberOfPagesInput.value.trim(); + const isBookRead = isBookReadCheckBox.checked; + if ( bookTitle == "" || bookAuthor == "" || bookNumberOfPages == "" ) { alert("Please fill all fields!"); return false; } else if ( - isNaN(Number(bookNumberOfPagesInput.value.trim())) || - !isValidInteger(Number(bookNumberOfPagesInput.value.trim())) || - isHexadecimal(bookNumberOfPagesInput.value.trim()) || - Number(bookNumberOfPagesInput.value.trim()) <= 0 + !isValidInteger(Number(bookNumberOfPages)) || + // if input hexadecimal (true) it will ask user to type correct number of pages format + isHexadecimal(bookNumberOfPages) || + Number(bookNumberOfPages) <= 0 ) { alert("Invalid number of pages format!"); return false; } else { - const bookTitle = sanitizeInput(bookTitleInput.value.trim()); - const bookAuthor = sanitizeInput(bookAuthorInput.value.trim()); - const bookNumberOfPages = parseInt(sanitizeInput(bookNumberOfPagesInput.value.trim()), 10); - let book = new Book(bookTitle, bookAuthor, bookNumberOfPages, isBookReadCheckBox.checked); + const bookNumberOfPagesInt = parseInt(bookNumberOfPagesInput.value.trim(), 10); + let book = new Book(bookTitle, bookAuthor, bookNumberOfPagesInt, isBookRead); myLibrary.push(book); render(); } @@ -97,11 +88,7 @@ function render() { changeReadStatusButton.className = "btn btn-success"; wasReadCell.appendChild(changeReadStatusButton); let readStatus = ""; - if (myLibrary[i].check == false) { - readStatus = "No"; - } else { - readStatus = "Yes"; - } + !myLibrary[i].check ? readStatus = "No" : readStatus = "Yes"; changeReadStatusButton.innerText = readStatus; changeReadStatusButton.addEventListener("click", function () { From 5ed9f1c4b12426c8179ebf171386a3deb7555861 Mon Sep 17 00:00:00 2001 From: TarasMykytiuk Date: Tue, 19 Aug 2025 16:24:59 +0100 Subject: [PATCH 6/6] reaction to feedback --- debugging/book-library/index.html | 4 ++-- debugging/book-library/script.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 50757c73..195daeb1 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -42,9 +42,9 @@

Library

name="author" required > - +