Skip to content
Closed
Changes from 1 commit
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
50 changes: 26 additions & 24 deletions debugging/book-library/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ window.addEventListener("load", function () {

function populateStorage() {
if (myLibrary.length === 0) {
let book1 = new Book("Robinson Crusoe", "Daniel Defoe", "252", true);
let book2 = new Book("The Old Man and the Sea", "Ernest Hemingway", "127", true);
let book1 = new Book("Robinson Crusoe", "Daniel Defoe", 252, true);
let book2 = new Book("The Old Man and the Sea", "Ernest Hemingway", 127, true);
myLibrary.push(book1, book2);
render();
}
Expand All @@ -27,41 +27,44 @@ const pages = document.getElementById("pages");
const check = document.getElementById("check");

function submit() {
if (
title.value.trim() === "" ||
author.value.trim() === "" ||
pages.value.trim() === ""
) {
alert("Please fill all fields!");
return false;
} else {
let book = new Book(title.value, author.value, pages.value, check.checked);
myLibrary.push(book);
render();
const trimmedTitle = title.value.trim();
const trimmedAuthor = author.value.trim();
const pagesNumber = Number(pages.value.trim());

// Clear form
title.value = "";
author.value = "";
pages.value = "";
check.checked = false;
if (!trimmedTitle || !trimmedAuthor || !pages.value || isNaN(pagesNumber) || pagesNumber <= 0) {
alert("Please fill all fields correctly!");
return false;
}

let book = new Book(trimmedTitle, trimmedAuthor, pagesNumber, check.checked);
Copy link
Contributor

Choose a reason for hiding this comment

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

pageNumber could be a value like 3.1416.

myLibrary.push(book);
render();

title.value = "";
author.value = "";
pages.value = "";
check.checked = false;
}

function render() {
let table = document.getElementById("display");

// Clear all rows except the header
for (let i = table.rows.length - 1; i > 0; i--) {
table.deleteRow(i);
}

myLibrary.forEach((book, i) => {
let row = table.insertRow(-1);
row.insertCell(0).innerText = book.title;
row.insertCell(1).innerText = book.author;
row.insertCell(2).innerText = book.pages;

let titleCell = row.insertCell(0);
titleCell.innerText = book.title;

let authorCell = row.insertCell(1);
authorCell.innerText = book.author;

let pagesCell = row.insertCell(2);
pagesCell.innerText = book.pages;

// Read status toggle button
let readCell = row.insertCell(3);
let readBtn = document.createElement("button");
readBtn.innerText = book.check ? "Yes" : "No";
Expand All @@ -72,7 +75,6 @@ function render() {
});
readCell.appendChild(readBtn);

// Delete button
let deleteCell = row.insertCell(4);
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you think of the pros and cons of these two approaches for creating cells within a row?

  • Keeping all the cell creation code in one location, like the original code does.
  • Scattering the cell creation code across different locations, like what you did.

Copy link
Author

Choose a reason for hiding this comment

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

Keeping it in one location will make it easier to read. I'll correct mine.

Copy link
Contributor

Choose a reason for hiding this comment

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

By "keeping all cell creation code in one location", I meant

    row.insertCell(0).innerText = book.title;
    row.insertCell(1).innerText = book.author;
    row.insertCell(2).innerText = book.pages;
    
    // Could use const instead of let
    const readCell = row.insertCell(3);        
    const deleteCell = row.insertCell(4);
    
    ...

This way, it is easy to find out there are 5 cells created.

let deleteBtn = document.createElement("button");
deleteBtn.innerText = "Delete";
Expand Down