From 5d4442d48d9c2a47d6fee962037d42ec0a93a226 Mon Sep 17 00:00:00 2001 From: Khaliun Baatarkhuu Date: Mon, 10 Aug 2026 22:36:34 +0100 Subject: [PATCH 1/6] Update input types for title and author fields Changed input types for title and author fields to 'text'. --- debugging/book-library/index.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 23acfa71..2628651d 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -31,7 +31,7 @@

Library

Library /> Date: Tue, 11 Aug 2026 08:25:08 +0100 Subject: [PATCH 2/6] Fix book submission and rendering logic --- debugging/book-library/script.js | 58 +++++++++++++++++++++----------- 1 file changed, 38 insertions(+), 20 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 75ce6c1d..c4a8e6c4 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -8,15 +8,16 @@ 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( "The Old Man and the Sea", "Ernest Hemingway", "127", true ); + myLibrary.push(book1); myLibrary.push(book2); - render(); } } @@ -25,22 +26,26 @@ const author = document.getElementById("author"); const pages = document.getElementById("pages"); const check = document.getElementById("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 || + author.value == "" || 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); - render(); } + + let book = new Book( + title.value, + author.value, + pages.value, + check.checked + ); + + myLibrary.push(book); + render(); } function Book(title, author, pages, check) { @@ -53,34 +58,44 @@ 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-- { + + + for (let n = rowsNumber - 1; n > 0; n--) { table.deleteRow(n); } - //insert updated row and cells + + let 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 == false) { + + if (myLibrary[i].check == true) { readStatus = "Yes"; } else { readStatus = "No"; } + changeBut.innerText = readStatus; changeBut.addEventListener("click", function () { @@ -88,13 +103,16 @@ function render() { 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; + delButton.className = "btn btn-warning"; + delButton.innerHTML = "Delete"; + + deleteCell.appendChild(delButton); + + delButton.addEventListener("click", function () { alert(`You've deleted title: ${myLibrary[i].title}`); myLibrary.splice(i, 1); render(); From cb6b3a7877b51a7edf09138e2ce67ca53f315669 Mon Sep 17 00:00:00 2001 From: Khaliun Date: Tue, 11 Aug 2026 13:43:59 +0100 Subject: [PATCH 3/6] Fix book library bugs --- debugging/book-library/index.html | 2 +- debugging/book-library/script.js | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 2628651d..f723f7e6 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -65,7 +65,7 @@

Library

type="submit" value="Submit" class="btn btn-primary" - onclick="submit();" + onclick="addBook();" />
diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index c4a8e6c4..4fd111bb 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -27,7 +27,7 @@ const pages = document.getElementById("pages"); const check = document.getElementById("check"); -function submit() { +function addBook() { if ( title.value == "" || author.value == "" || @@ -45,6 +45,12 @@ function submit() { ); myLibrary.push(book); + + title.value = ""; + author.value = ""; + pages.value = ""; + check.checked = false; + render(); } From 6b83303cf93e3d7c189fb4635e640665e30fc748 Mon Sep 17 00:00:00 2001 From: Khaliun Date: Wed, 12 Aug 2026 12:15:32 +0100 Subject: [PATCH 4/6] Improve book library code --- debugging/book-library/script.js | 80 ++++++++++++++------------------ 1 file changed, 35 insertions(+), 45 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 4fd111bb..72fbbd71 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -7,12 +7,12 @@ window.addEventListener("load", function (e) { function populateStorage() { if (myLibrary.length == 0) { - let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true); + const book1 = new Book("Robison Crusoe", "Daniel Defoe", 252, true); - let book2 = new Book( + const book2 = new Book( "The Old Man and the Sea", "Ernest Hemingway", - "127", + 127, true ); @@ -21,39 +21,38 @@ function populateStorage() { } } -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 readInput = document.getElementById("check"); + function addBook() { if ( - title.value == "" || - author.value == "" || - pages.value == "" + titleInput.value == "" || + authorInput.value == "" || + pagesInput.value == "" ) { alert("Please fill all fields!"); return false; } - let book = new Book( - title.value, - author.value, - pages.value, - check.checked + const book = new Book( + titleInput.value, + authorInput.value, + Number(pagesInput.value), + readInput.checked ); myLibrary.push(book); - title.value = ""; - author.value = ""; - pages.value = ""; - check.checked = false; + titleInput.value = ""; + authorInput.value = ""; + pagesInput.value = ""; + readInput.checked = false; render(); } - function Book(title, author, pages, check) { this.title = title; this.author = author; @@ -62,8 +61,8 @@ function Book(title, author, pages, check) { } function render() { - let table = document.getElementById("display"); - let rowsNumber = table.rows.length; + const table = document.getElementById("display"); + const rowsNumber = table.rows.length; for (let n = rowsNumber - 1; n > 0; n--) { @@ -71,36 +70,28 @@ function render() { } - let length = myLibrary.length; + const length = myLibrary.length; for (let i = 0; i < length; i++) { - let row = table.insertRow(1); + const 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 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; - let changeBut = document.createElement("button"); + const 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"; - } + const readStatus = myLibrary[i].check ? "Yes" : "No"; changeBut.innerText = readStatus; @@ -110,9 +101,8 @@ function render() { }); - let delButton = document.createElement("button"); + const delButton = document.createElement("button"); - delButton.id = i + 5; delButton.className = "btn btn-warning"; delButton.innerHTML = "Delete"; From e7089ffe3a6a0ba33fe1446537b5343ba945cc13 Mon Sep 17 00:00:00 2001 From: Khaliun Date: Wed, 12 Aug 2026 23:21:27 +0100 Subject: [PATCH 5/6] Address reviewer feedback --- debugging/book-library/index.html | 63 +++++++++++++++-------------- debugging/book-library/script.js | 67 +++++++++++++++++-------------- 2 files changed, 69 insertions(+), 61 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index f723f7e6..d0e5f12b 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,20 +1,22 @@ - + - + My Book Library + + > + + - + > + @@ -28,7 +30,7 @@

Library

-
+
Library id="title" name="title" required - /> - + > + + + > + -
+ + +
@@ -80,17 +88,10 @@

Library

- - - - - - - - - + +
- + \ No newline at end of file diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 72fbbd71..fe10a209 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(); @@ -20,36 +20,45 @@ function populateStorage() { myLibrary.push(book2); } } - const titleInput = document.getElementById("title"); const authorInput = document.getElementById("author"); const pagesInput = document.getElementById("pages"); const readInput = document.getElementById("check"); - + +const bookForm = document.getElementById("bookForm"); + +bookForm.addEventListener("submit", function (event) { + event.preventDefault(); + addBook(); +}); function addBook() { - if ( - titleInput.value == "" || - authorInput.value == "" || - pagesInput.value == "" - ) { - alert("Please fill all fields!"); - return false; - } + const title = titleInput.value.trim(); + const author = authorInput.value.trim(); + const pages = Number(pagesInput.value); + +if ( + title === "" || + author === "" || + !Number.isFinite(pages) || + pages < 1 || + !Number.isInteger(pages) +) { + + alert("Please fill all fields correctly!"); + return false; +} const book = new Book( - titleInput.value, - authorInput.value, - Number(pagesInput.value), + title, + author, + pages, readInput.checked - ); +); myLibrary.push(book); - titleInput.value = ""; - authorInput.value = ""; - pagesInput.value = ""; - readInput.checked = false; + bookForm.reset(); render(); } @@ -61,19 +70,14 @@ function Book(title, author, pages, check) { } function render() { - const table = document.getElementById("display"); - const rowsNumber = table.rows.length; + const bookList = document.getElementById("bookList"); - - for (let n = rowsNumber - 1; n > 0; n--) { - table.deleteRow(n); - } + bookList.innerHTML = ""; - const length = myLibrary.length; for (let i = 0; i < length; i++) { - const row = table.insertRow(1); + const row = bookList.insertRow(); const titleCell = row.insertCell(0); const authorCell = row.insertCell(1); @@ -93,7 +97,7 @@ function render() { const readStatus = myLibrary[i].check ? "Yes" : "No"; - changeBut.innerText = readStatus; + changeBut.textContent = readStatus; changeBut.addEventListener("click", function () { myLibrary[i].check = !myLibrary[i].check; @@ -104,14 +108,17 @@ function render() { const delButton = document.createElement("button"); delButton.className = "btn btn-warning"; - delButton.innerHTML = "Delete"; + delButton.textContent = "Delete"; deleteCell.appendChild(delButton); delButton.addEventListener("click", function () { - alert(`You've deleted title: ${myLibrary[i].title}`); + const deletedTitle = myLibrary[i].title; + myLibrary.splice(i, 1); render(); + + alert(`You've deleted title: ${deletedTitle}`); }); } } From 53167662ea67cd01844dfeabe5a134ead7905b89 Mon Sep 17 00:00:00 2001 From: Khaliun Baatarkhuu Date: Thu, 13 Aug 2026 13:34:58 +0100 Subject: [PATCH 6/6] Delay alert after book deletion Change alert to use setTimeout for better UX. --- debugging/book-library/script.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index fe10a209..588eecbf 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -118,7 +118,9 @@ function render() { myLibrary.splice(i, 1); render(); - alert(`You've deleted title: ${deletedTitle}`); + setTimeout(function () { + alert(`You've deleted title: ${deletedTitle}`); + }, 0); }); } }