-
-
Notifications
You must be signed in to change notification settings - Fork 162
Manchester | 25-ITP-May | Jennifer Isidienu | Sprint 2 | Book-Library #314
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 1 commit
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 |
|---|---|---|
|
|
@@ -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(); | ||
| } | ||
|
|
@@ -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); | ||
| 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"; | ||
|
|
@@ -72,7 +75,6 @@ function render() { | |
| }); | ||
| readCell.appendChild(readBtn); | ||
|
|
||
| // Delete button | ||
| let deleteCell = row.insertCell(4); | ||
|
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. Can you think of the pros and cons of these two approaches for creating cells within a row?
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. Keeping it in one location will make it easier to read. I'll correct mine.
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. By "keeping all cell creation code in one location", I meant This way, it is easy to find out there are 5 cells created. |
||
| let deleteBtn = document.createElement("button"); | ||
| deleteBtn.innerText = "Delete"; | ||
|
|
||
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.
pageNumbercould be a value like3.1416.