-
-
Notifications
You must be signed in to change notification settings - Fork 161
LONDON | MAY 2025 | JESUS DEL MORAL | DATA FLOWS | BOOK LIBRARY #302
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
|
|
||
|
|
||
| let myLibrary = []; | ||
|
|
||
| window.addEventListener("load", function (e) { | ||
|
|
@@ -7,16 +9,15 @@ window.addEventListener("load", function (e) { | |
|
|
||
| function populateStorage() { | ||
| if (myLibrary.length == 0) { | ||
| let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true); | ||
| let book1 = new Book("Robison Crusoe", "Daniel Defoe", 252, true); | ||
| let book2 = new Book( | ||
| "The Old Man and the Sea", | ||
| "Ernest Hemingway", | ||
| "127", | ||
| 127, | ||
| true | ||
| ); | ||
| myLibrary.push(book1); | ||
| myLibrary.push(book2); | ||
| render(); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -28,19 +29,27 @@ 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 == "" | ||
| ) { | ||
| alert("Please fill all fields!"); | ||
| const cleanTitle = title.value.trim(); | ||
| const cleanAuthor = author.value.trim(); | ||
|
|
||
| if (!/^[1-9]\d*$/.test(pages.value.trim())) { // plain positive whole numbers like 1, 25, 300 — no letters, no decimal points, no scientific notation | ||
| alert("Pages must be a positive whole number without letters or symbols."); | ||
| return false; | ||
| } else { | ||
| let book = new Book(title.value, title.value, pages.value, check.checked); | ||
| library.push(book); | ||
| render(); | ||
| } | ||
|
|
||
| const cleanPages = parseInt(pages.value.trim(), 10); | ||
|
|
||
| if (!cleanTitle|| !cleanAuthor) { // title and author doesn't allowed to contain only space characters | ||
| alert("Title and Author cannot be empty or spaces only."); | ||
| return false; | ||
|
|
||
| } if (isNaN(cleanPages) || cleanPages <= 0) { // the value of type of pages must be a integer | ||
| alert("Pages must be a positive whole number."); | ||
| return false; | ||
| } | ||
| let book = new Book(cleanTitle, cleanAuthor, pages.value, check.checked); //// title and author be allowed to contain leading or trailing space characters, we storage using parseInt | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cjyuan
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not use |
||
| myLibrary.push(book); | ||
| render(); | ||
| } | ||
|
|
||
| function Book(title, author, pages, check) { | ||
|
|
@@ -52,11 +61,10 @@ 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); | ||
| } | ||
| // Keep only the header row | ||
| table.innerHTML = table.rows[0].outerHTML; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since the An alternative approach is the clear
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No change?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I removed from index.html |
||
|
|
||
| //insert updated row and cells | ||
| let length = myLibrary.length; | ||
| for (let i = 0; i < length; i++) { | ||
|
|
@@ -66,37 +74,35 @@ function render() { | |
| 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; | ||
| titleCell.textContent = myLibrary[i].title; | ||
| authorCell.textContent = myLibrary[i].author; | ||
| pagesCell.textContent = 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 toggleReadButton = document.createElement("button"); | ||
| toggleReadButton.className = "btn btn-success"; | ||
| wasReadCell.appendChild(toggleReadButton); | ||
| let readStatus = ""; | ||
| if (myLibrary[i].check == false) { | ||
| readStatus = "Yes"; | ||
| } else { | ||
| readStatus = "No"; | ||
| } | ||
| changeBut.innerText = readStatus; | ||
|
|
||
| changeBut.addEventListener("click", function () { | ||
|
|
||
| readStatus = myLibrary[i].check ? "Yes" : "No"; | ||
|
|
||
| toggleReadButton.innerText = readStatus; | ||
|
|
||
| toggleReadButton.addEventListener("click", function () { | ||
| myLibrary[i].check = !myLibrary[i].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); | ||
| let deleteBookButton = document.createElement("button"); | ||
| deleteCell.appendChild(deleteBookButton); | ||
| deleteBookButton.className = "btn btn-warning"; | ||
| deleteBookButton.innerHTML = "Delete"; | ||
| deleteBookButton.addEventListener("click", function () { | ||
| const deleteTitle = myLibrary[i].title; //store first | ||
| myLibrary.splice(i, 1); // delete it | ||
| alert(`You have deleted title: ${deleteTitle}`); | ||
| render(); | ||
| }); | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Without
else, common practice is to start anifblock on a separate line.