From 2f400c483e089cd22b1d91c4f086d522d14009fc Mon Sep 17 00:00:00 2001 From: Yonatan Teklemariam Date: Sat, 8 Aug 2026 17:51:27 +0100 Subject: [PATCH 1/4] debugged the book library js file and fixed the bugs in it --- debugging/book-library/script.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 75ce6c1d..7e845516 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -37,8 +37,8 @@ function submit() { 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 +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 @@ -76,7 +76,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"; @@ -90,11 +90,11 @@ function render() { //add delete button to every row and render again let delButton = document.createElement("button"); - delBut.id = i + 5; - deleteCell.appendChild(delBut); - delBut.className = "btn btn-warning"; - delBut.innerHTML = "Delete"; - delBut.addEventListener("clicks", function () { + delButton.id = i + 5; + deleteCell.appendChild(delButton); + delButton.className = "btn btn-warning"; + delButton.innerHTML = "Delete"; + delButton.addEventListener("click", function () { alert(`You've deleted title: ${myLibrary[i].title}`); myLibrary.splice(i, 1); render(); From b9bc3cbbd6270544fd07c2f6f3c170900429e4f3 Mon Sep 17 00:00:00 2001 From: Yonatan Teklemariam Date: Tue, 11 Aug 2026 07:36:13 +0100 Subject: [PATCH 2/4] validated the index.html in the w3 validator --- debugging/book-library/index.html | 152 ++++++++++++------------------ 1 file changed, 62 insertions(+), 90 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 23acfa71..7a680fc3 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,96 +1,68 @@ - - - - - - - - - - - + + - -
-

Library

-

Add books to your virtual library

-
+ + Book Library + + + + + + + + + + +
+

Library

+

Add books to your virtual library

+
- + -
-
- - - - - - - - -
+
+
+ + +
+
+
+
+ + +
+ + +
+ + + + + + + + + + + + + + + + + + + + +
TitleAuthorNumber of PagesRead
- - - - - - - - - - - - - - - - - - - -
TitleAuthorNumber of PagesRead
+ + - - - + \ No newline at end of file From 633d88d6f8aaf03a63973d42e2a408e442a3f78e Mon Sep 17 00:00:00 2001 From: Yonatan Teklemariam Date: Tue, 11 Aug 2026 09:48:33 +0100 Subject: [PATCH 3/4] debugged the css, script and index against the guidelines for possible improvement --- debugging/book-library/index.html | 2 +- debugging/book-library/script.js | 156 +++++++++++++++++++----------- debugging/book-library/style.css | 1 - 3 files changed, 100 insertions(+), 59 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 7a680fc3..8f592de8 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -62,7 +62,7 @@

Library

- + \ No newline at end of file diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 7e845516..45e8761d 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 (e) { populateStorage(); @@ -7,40 +7,61 @@ window.addEventListener("load", function (e) { function populateStorage() { if (myLibrary.length == 0) { - let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true); - let book2 = new Book( + const book1 = new Book("Robison 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); - render(); + myLibrary.push(book1, book2); } } -const title = document.getElementById("title"); -const author = document.getElementById("author"); -const pages = document.getElementById("pages"); -const check = document.getElementById("check"); +const titleInput = document.getElementById("title"); +const authorInput = document.getElementById("author"); +const pagesInput = document.getElementById("pages"); +const checkInput = document.getElementById("check"); + +//reset the form after submit +const formReset = () => { + titleInput.value = ""; + authorInput.value = ""; + pagesInput.value = ""; + checkInput.checked = false; + $("#demo").collapse("hide"); +}; //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() { + const titleValue = titleInput.value.trim(); + const authorValue = authorInput.value.trim(); + const pagesValue = pagesInput.value.trim(); + const pagesNumber = Number(pagesValue); + if ( - title.value == null || - title.value == "" || - pages.value == null || - pages.value == "" + !titleValue || + !authorValue || + !pagesValue || + isNaN(pagesNumber) || + pagesNumber <= 0 || + !Number.isInteger(pagesNumber) ) { - alert("Please fill all fields!"); + userNotification("Please fill all fields!"); return false; - } else { - let book = new Book(title.value, author.value, pages.value, check.checked); - myLibrary.push(book); - render(); } + + const book = new Book( + titleValue, + authorValue, + pagesNumber, + checkInput.checked + ); + + myLibrary.push(book); + formReset(); + render(); } function Book(title, author, pages, check) { @@ -51,53 +72,74 @@ 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); - } + const booksTable = document.querySelector("#display tbody"); + booksTable.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; - - //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 row = booksTable.insertRow(); + 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; + + //read/unread button status + const readButton = document.createElement("button"); + readButton.className = "btn btn-success"; + readButton.textContent = myLibrary[i].check ? "Yes" : "No"; + + readButton.addEventListener("click", function () { myLibrary[i].check = !myLibrary[i].check; render(); }); + wasReadCell.appendChild(readButton); - //add delete button to every row and render again - let delButton = document.createElement("button"); - delButton.id = i + 5; - deleteCell.appendChild(delButton); + //delete button to every row and render again + const delButton = document.createElement("button"); delButton.className = "btn btn-warning"; delButton.innerHTML = "Delete"; delButton.addEventListener("click", function () { - alert(`You've deleted title: ${myLibrary[i].title}`); - myLibrary.splice(i, 1); + const deletedBook = myLibrary.splice(i, 1)[0]; render(); + // alert message when book is deleted + userNotification( + `You have deleted the book: ${deletedBook.title} by ${deletedBook.author}.` + ); }); + deleteCell.appendChild(delButton); + } +} + +function userNotification(message) { + let note = document.getElementById("notification"); + + if (!note) { + note = document.createElement("div"); + note.id = "notification"; + note.style.cssText = ` + background: #e0f3ff; + border: 1px solid #9ac7e0; + padding: 10px; + margin-top: 10px; + border-radius: 4px; + font-size: 14px; + `; + document.body.prepend(note); } + + note.textContent = message; + + setTimeout(() => note.remove(), 3000); } +// Attach submit handler to the form +document + .querySelector('input[type="submit"]') + .addEventListener("click", function (e) { + e.preventDefault(); + submit(); + }); diff --git a/debugging/book-library/style.css b/debugging/book-library/style.css index 302950cb..c66eb0cf 100644 --- a/debugging/book-library/style.css +++ b/debugging/book-library/style.css @@ -1,6 +1,5 @@ .form-group { width: 400px; - height: 300px; align-self: left; padding-left: 20px; } From 35eb18078c19767e3919f084691fc14cc963cd12 Mon Sep 17 00:00:00 2001 From: Yonatan Teklemariam Date: Tue, 11 Aug 2026 14:50:43 +0100 Subject: [PATCH 4/4] all shared variables sit at the top, added bookForm.reset(), and removed redundant validation for the browser handles validation on its own.. --- debugging/book-library/index.html | 35 +++++---- debugging/book-library/script.js | 117 +++++++++++++----------------- 2 files changed, 70 insertions(+), 82 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 8f592de8..c30c3c49 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -23,23 +23,26 @@

Library

-
- - -
-
- -
-
- - -
- - +
+
+ + +
+
+ +
+
+ + +
+ +
+ + diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 45e8761d..da5f7c1f 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,10 +1,47 @@ +// 1. Shared data const myLibrary = []; -window.addEventListener("load", function (e) { - populateStorage(); - render(); -}); +// 2. Shared DOM references +const titleInput = document.getElementById("title"); +const authorInput = document.getElementById("author"); +const pagesInput = document.getElementById("pages"); +const checkInput = document.getElementById("check"); +const bookForm = document.getElementById("book-form"); + +// 3. Utility functions +function Book(title, author, pages, check) { + this.title = title; + this.author = author; + this.pages = pages; + this.check = check; +} + +function userNotification(message) { + let note = document.getElementById("notification"); + if (!note) { + note = document.createElement("div"); + note.id = "notification"; + note.style.cssText = ` + background: #e0f3ff; + border: 1px solid #9ac7e0; + padding: 10px; + margin-top: 10px; + border-radius: 4px; + font-size: 14px; + `; + document.body.prepend(note); + } + + note.textContent = message; + setTimeout(() => note.remove(), 3000); +} + +const formReset = () => { + bookForm.reset(); +}; + +// 4. Core logic function populateStorage() { if (myLibrary.length == 0) { const book1 = new Book("Robison Crusoe", "Daniel Defoe", 252, true); @@ -18,36 +55,13 @@ function populateStorage() { } } -const titleInput = document.getElementById("title"); -const authorInput = document.getElementById("author"); -const pagesInput = document.getElementById("pages"); -const checkInput = document.getElementById("check"); - -//reset the form after submit -const formReset = () => { - titleInput.value = ""; - authorInput.value = ""; - pagesInput.value = ""; - checkInput.checked = false; - $("#demo").collapse("hide"); -}; - -//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() { const titleValue = titleInput.value.trim(); const authorValue = authorInput.value.trim(); const pagesValue = pagesInput.value.trim(); const pagesNumber = Number(pagesValue); - if ( - !titleValue || - !authorValue || - !pagesValue || - isNaN(pagesNumber) || - pagesNumber <= 0 || - !Number.isInteger(pagesNumber) - ) { + if (!titleValue || !authorValue || !pagesValue) { userNotification("Please fill all fields!"); return false; } @@ -58,55 +72,42 @@ function submit() { pagesNumber, checkInput.checked ); - myLibrary.push(book); formReset(); render(); } -function Book(title, author, pages, check) { - this.title = title; - this.author = author; - this.pages = pages; - this.check = check; -} - function render() { const booksTable = document.querySelector("#display tbody"); booksTable.innerHTML = ""; - //insert updated row and cells - const length = myLibrary.length; - for (let i = 0; i < length; i++) { + for (let i = 0; i < myLibrary.length; i++) { const row = booksTable.insertRow(); 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; - //read/unread button status const readButton = document.createElement("button"); readButton.className = "btn btn-success"; readButton.textContent = myLibrary[i].check ? "Yes" : "No"; - readButton.addEventListener("click", function () { myLibrary[i].check = !myLibrary[i].check; render(); }); wasReadCell.appendChild(readButton); - //delete button to every row and render again const delButton = document.createElement("button"); delButton.className = "btn btn-warning"; delButton.innerHTML = "Delete"; delButton.addEventListener("click", function () { const deletedBook = myLibrary.splice(i, 1)[0]; render(); - // alert message when book is deleted userNotification( `You have deleted the book: ${deletedBook.title} by ${deletedBook.author}.` ); @@ -115,31 +116,15 @@ function render() { } } -function userNotification(message) { - let note = document.getElementById("notification"); +// 5. Event listeners - if (!note) { - note = document.createElement("div"); - note.id = "notification"; - note.style.cssText = ` - background: #e0f3ff; - border: 1px solid #9ac7e0; - padding: 10px; - margin-top: 10px; - border-radius: 4px; - font-size: 14px; - `; - document.body.prepend(note); - } - - note.textContent = message; +// 6. Initialization +window.addEventListener("load", function () { + populateStorage(); + render(); - setTimeout(() => note.remove(), 3000); -} -// Attach submit handler to the form -document - .querySelector('input[type="submit"]') - .addEventListener("click", function (e) { + bookForm.addEventListener("submit", function (e) { e.preventDefault(); submit(); }); +});