Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions debugging/book-library/index.html

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some things missing from your HTML file that should be added. Hint: how people are going to interact with your webpage in a web browser?

There is also some invalid HTML. Can you spot these? Hint: have you used an HTML validator before? https://validator.w3.org/

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@LonMcGregor this was actually a lot. Really useful site. So have update the file, please review and let me know if more is required.

Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ <h1>Library</h1>
<div class="form-group">
<label for="title">Title:</label>
<input
type="title"
type="text"
class="form-control"
id="title"
name="title"
required
/>
<label for="author">Author: </label>
<input
type="author"
type="text"
class="form-control"
id="author"
name="author"
Expand Down
128 changes: 55 additions & 73 deletions debugging/book-library/script.js
Original file line number Diff line number Diff line change
@@ -1,103 +1,85 @@
// array
let myLibrary = [];

window.addEventListener("load", function (e) {
populateStorage();
render();
});

// form elements
const titleInput = document.getElementById("title");
const authorInput = document.getElementById("author");
const pagesInput = document.getElementById("pages");
const checkInput = document.getElementById("check");
// populate the library with some default books if it's empty
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
);
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();
}
}
}

const title = document.getElementById("title");
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 ||
pages.value == ""
titleInput.value.trim == "" || // cant be null so removed all that

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There may be ways to improve your form validation here. Things to think about:

  • Is your validation checking all the inputs?
  • Is there a way to get the browser to do some of the checking for you, without needing to manually check for empty inputs?

Copy link
Author

@Dave-Vermeulen Dave-Vermeulen Aug 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@LonMcGregor ah very DRY indeed. cool HTML5 tricks. have updated both files to handle the validation and submission a bit differently.

authorInput.value.trim == "" ||
pagesInput.value.trim == ""
) {
alert("Please fill all fields!");
return false;
} else {
let book = new Book(title.value, title.value, pages.value, check.checked);
library.push(book);
let newBook = new Book(titleInput.value, authorInput.value, parseInt(pagesInput.value), checkInput.checked);
myLibrary.push(newBook);
//clear form after adding newBook
titleInput.value = '';
authorInput.value = '';
pagesInput.value = '';
checkInput.value = false;
render();
}
}

// book constructor
function Book(title, author, pages, check) {
this.title = title;
this.author = author;
this.pages = pages;
this.check = check;
}

// renders lib data into HTML
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 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) {
readStatus = "Yes";
} else {
readStatus = "No";
}
changeBut.innerText = readStatus;
let tableBody = document.querySelector("#display tbody");
tableBody.innerHTML = ''; // get rid of all instead of one by one row deletion

myLibrary.forEach((book, index) => {
let row = tableBody.insertRow();
// post debate lets use textContent instead of innerHTML
row.insertCell(0).textContent = book.title;
row.insertCell(1).textContent = book.author;
row.insertCell(2).textContent = book.pages;
// 'read' status
const wasReadCell = row.insertCell(3);
const readStatusBtn = document.createElement("button");
readStatusBtn.className = "btn btn-success";
readStatusBtn.textContent = book.check ? "Yes" : "No";
wasReadCell.appendChild(readStatusBtn);

changeBut.addEventListener("click", function () {
myLibrary[i].check = !myLibrary[i].check;
readStatusBtn.addEventListener("click", function () {
book.check = !book.check;
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 () {
alert(`You've deleted title: ${myLibrary[i].title}`);
myLibrary.splice(i, 1);
const deleteCell = row.insertCell(4);
const deleteBtn = document.createElement("button");
deleteBtn.className = "btn btn-warning";
deleteBtn.innerHTML = "Delete";
deleteCell.appendChild(deleteBtn);
deleteBtn.addEventListener("click", function () {
const deletedTitle = myLibrary[index].title;
myLibrary.splice(index, 1);
render();
alert(`You've deleted title: ${deletedTitle}`);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think about how deleting a file works on your desktop. Is being notified after the deletion normally how this works? Is there a better interaction you could use than an alert here?

Copy link
Author

@Dave-Vermeulen Dave-Vermeulen Aug 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@LonMcGregor okay so this is bit higher grade. i think what you mean is that the users flow is interrupted when the alert pops up (clarify if im wrong kanala). based on this assumption and the fact that the delete works i have built a new function to display the notification and done away with the disruptive alert. The new one disappears after 3 secs.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The alert is quite interrupting, yes, making it less "in your face" is one solution. The other is, if you really want something interrupting, to use something like confirm instead.

It sounds like you've made good progress here. I can't see the files yet on github though - have you comitted them all?

});
}
});
}
// moved to end to make sure all is defined before load
window.addEventListener("load", function () {
populateStorage();
render();
});