From c6cbe6c4ceb5a23f44f31e9d4fa47607d8dcb36a Mon Sep 17 00:00:00 2001 From: Jamal Laqdiem Date: Wed, 30 Jul 2025 17:44:04 +0100 Subject: [PATCH 1/5] Added extra books, adjusted the typos in the code. --- .vscode/settings.json | 3 +++ debugging/book-library/script.js | 20 +++++++++++++++----- 2 files changed, 18 insertions(+), 5 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..6f3a2913 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "liveServer.settings.port": 5501 +} \ No newline at end of file diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 75ce6c1d..9e5d5889 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -14,8 +14,12 @@ function populateStorage() { "127", true ); + let book3 = new Book("Pride and Prejudice","Jane Austen","147", false); + let book4 = new Book("Rich dad Poor dad"," Robert Kiyosaki", "336", true); myLibrary.push(book1); myLibrary.push(book2); + myLibrary.push(book3); + myLibrary.push(book4) render(); } } @@ -38,8 +42,12 @@ function submit() { return false; } else { let book = new Book(title.value, title.value, pages.value, check.checked); - library.push(book); + myLibrary.push(book); render(); + title.value =""; + author.value = ""; + pages.value = ""; + check.checked = false } } @@ -48,13 +56,14 @@ function Book(title, author, pages, check) { this.author = author; this.pages = pages; this.check = 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 @@ -76,7 +85,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,15 +98,16 @@ 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 e4c200309f41b5c2c96b5cbf03978c5d96146a29 Mon Sep 17 00:00:00 2001 From: Jamal Laqdiem Date: Thu, 31 Jul 2025 10:35:30 +0100 Subject: [PATCH 2/5] Added the check for author --- debugging/book-library/script.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 9e5d5889..99c1490b 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -35,19 +35,21 @@ 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); + let book = new Book(title.value,author.value, pages.value, check.checked); myLibrary.push(book); render(); title.value =""; author.value = ""; pages.value = ""; - check.checked = false + check.checked = false; } } @@ -81,7 +83,7 @@ function render() { //add and wait for action for read/unread button let changeBut = document.createElement("button"); - changeBut.id = i; + changeBut.id = i+5; changeBut.className = "btn btn-success"; wasReadCell.appendChild(changeBut); let readStatus = ""; From f22abb360cb2d3757006c6e030869d9a48f5221c Mon Sep 17 00:00:00 2001 From: Jamal Laqdiem Date: Sun, 10 Aug 2025 12:11:33 +0100 Subject: [PATCH 3/5] Applied the feedback to improve and test my index.html and my script.js to handle and pass all criteria requested, answering questions in form of comments. --- debugging/book-library/index.html | 11 +++- debugging/book-library/script.js | 92 +++++++++++++++++-------------- 2 files changed, 58 insertions(+), 45 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 23acfa71..e06f7e4e 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,7 +1,12 @@ - + + Book Library Library
Library /> add the new book (object in array) //via Book function and start render function function submit() { + // the .value is a string. + // we do not need to check if the .value is null as input element will always return a string even empty one. + let pagesToNumber = Number(inputPages.value) if ( - title.value == null || - title.value == "" || - author.value == null || - author.value == "" || - pages.value == null || - pages.value == "" + inputTitle.value.trim() === "" || + inputAuthor.value.trim() === "" || + isNaN(pagesToNumber) || + pagesToNumber<= 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(inputTitle.value,inputAuthor.value, pagesToNumber, check.checked); myLibrary.push(book); render(); - title.value =""; - author.value = ""; - pages.value = ""; + inputTitle.value =""; + inputAuthor.value = ""; + inputPages.value = ""; check.checked = false; } } @@ -62,50 +62,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); - } - //insert updated row and cells + let tableBody = document.getElementById("display").getElementsByTagName("tbody")[0]; + tableBody.innerHTML = '' + 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); + titleCell.textContent = myLibrary[i].title; let authorCell = row.insertCell(1); + authorCell.textContent = myLibrary[i].author; let pagesCell = row.insertCell(2); + pagesCell.textContent = myLibrary[i].pages; 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; + //change from innerHTML to textContent to ensures that user input is treated as plain text and not as executable HTML, preventing XSS attacks //add and wait for action for read/unread button - let changeBut = document.createElement("button"); - changeBut.id = i+5; - changeBut.className = "btn btn-success"; - wasReadCell.appendChild(changeBut); + let changeReadStatusBtn= document.createElement("button"); + changeReadStatusBtn.className = "btn btn-success"; + wasReadCell.appendChild(changeReadStatusBtn); let readStatus = ""; if (myLibrary[i].check == true) { readStatus = "Yes"; } else { readStatus = "No"; } - changeBut.innerText = readStatus; + changeReadStatusBtn.textContent = readStatus; - changeBut.addEventListener("click", function () { + changeReadStatusBtn.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 () { + let deleteBtn = document.createElement("button"); + deleteCell.appendChild(deleteBtn); + deleteBtn .className = "btn btn-warning"; + deleteBtn .textContent = "Delete"; + deleteBtn .addEventListener("click", function () { alert(`You've deleted title: ${myLibrary[i].title}`); myLibrary.splice(i, 1); render(); @@ -113,3 +109,15 @@ function render() { } } +//Questions : +//Should title and author be allowed to contain only space characters leading or trailing space characters? +//Yes, title and author fields can contain leading or trailing space characters, but these should be trimmed before the data is stored or displayed. + +//What type of value should we use to store the page count? +// the input field, which is a string, should be converted to a number using a function like Number() or parseInt() + +//What kinds of input values should be rejected? +//1. Empty or whitespace-only strings for the title and author. +//2. Non-numeric values for the page count. +//3. Zero or negative numbers for the page count. +//4. harmful characters or scripts. From 9e76c070ee0824f87077d1b4317b752f6e79c7ca Mon Sep 17 00:00:00 2001 From: Jamal Laqdiem Date: Sun, 10 Aug 2025 12:19:19 +0100 Subject: [PATCH 4/5] fixing the meta tags. --- debugging/book-library/index.html | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index e06f7e4e..a301d78c 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,17 +1,9 @@ - + + Book Library - From f02b3dddfade5d1f676cc6745e830dea01ab0156 Mon Sep 17 00:00:00 2001 From: Jamal Laqdiem Date: Sun, 10 Aug 2025 16:37:31 +0100 Subject: [PATCH 5/5] fix the requested issues. --- debugging/book-library/script.js | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 67487a69..6f21cb64 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -33,17 +33,22 @@ const check = document.getElementById("check"); function submit() { // the .value is a string. // we do not need to check if the .value is null as input element will always return a string even empty one. - let pagesToNumber = Number(inputPages.value) + let sanitizedTitle = inputTitle.value.trim(); + let sanitizedAuthor = inputAuthor.value.trim(); + let pagesToNumber = Number(inputPages.value); + // using Math.round to handle decimals. + let sanitizedPages = Math.round(pagesToNumber); + if ( - inputTitle.value.trim() === "" || - inputAuthor.value.trim() === "" || - isNaN(pagesToNumber) || - pagesToNumber<= 0 + sanitizedTitle=== "" || + sanitizedAuthor=== "" || + isNaN(sanitizedPages) || + sanitizedPages<= 0 ) { alert("Please fill all fields!"); return false; } else { - let book = new Book(inputTitle.value,inputAuthor.value, pagesToNumber, check.checked); + let book = new Book(sanitizedTitle,sanitizedAuthor, sanitizedPages, check.checked); myLibrary.push(book); render(); inputTitle.value =""; @@ -83,12 +88,8 @@ function render() { let changeReadStatusBtn= document.createElement("button"); changeReadStatusBtn.className = "btn btn-success"; wasReadCell.appendChild(changeReadStatusBtn); - let readStatus = ""; - if (myLibrary[i].check == true) { - readStatus = "Yes"; - } else { - readStatus = "No"; - } + + const readStatus = myLibrary[i].check ? 'Yes' : 'No' changeReadStatusBtn.textContent = readStatus; changeReadStatusBtn.addEventListener("click", function () { @@ -102,9 +103,9 @@ function render() { deleteBtn .className = "btn btn-warning"; deleteBtn .textContent = "Delete"; deleteBtn .addEventListener("click", function () { - alert(`You've deleted title: ${myLibrary[i].title}`); myLibrary.splice(i, 1); render(); + alert(`You've deleted title: ${myLibrary[i].title}`); }); } }