-
-
Notifications
You must be signed in to change notification settings - Fork 125
London | 25-ITP-May | Houssam Lahlah | Sprint 2 | Feature/book library #322
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
base: main
Are you sure you want to change the base?
Conversation
- change library.push(book) to myLibrary.push(book). - add a ) to the end of the for loop condition to avoid syntax error. - change myLibrary[i].check to myLibrary[i].check == true to check if the book is read or not. - change delBut to delButton because we declare it. - The event listener is "clicks" instead of "click".
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.
Can you check if any of this general feedback can help you further improve your code?
https://github.com/cjyuan/Module-Data-Flows/blob/book-library-feedback/debugging/book-library/feedback.md
Doing so can help me speed up the review process. Thanks.
…jects empty or invalid inputs
debugging/book-library/script.js
Outdated
const pagesVal = Number(pagesEl.value); | ||
|
||
if (!titleVal || !authorVal || !pagesVal) { | ||
alert("Please fill all fields!"); | ||
return false; | ||
} else { | ||
let book = new Book(title.value, title.value, pages.value, check.checked); | ||
library.push(book); | ||
render(); | ||
return; | ||
} | ||
|
||
const book = new Book(titleVal, authorVal, pagesVal, checkEl.checked); |
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.
Input could be any kind of numbers but some of them should not be considered a valid page count.
Can you add code to sanitize or reject these invalid value?
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.
Hi cjyuan,
Thank you for your guidence. you absoloutly right, I've add another if condition to handle the page number only and now submitBook() function validate the page count input.
- It now rejects invalid values such as 0, negative numbers, non-numeric input, or decimals.
- Only positive whole numbers are accepted as valid page counts.
- Users will see a clear alert message when their input doesn’t meet these rules.
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.
Have you tested the values which you said the app would reject? The modified code does not quite function the way you described here.
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.
Hi cjyuan,
Thanks for catching that — I realized the behavior I described earlier didn’t fully match what was in the code (my mistake!).
I’ve now updated the validation to use a regex (/^\d+$/) instead of the simple Number() check. This ensures only positive whole numbers are accepted.
I tested with "0", "-5", and "12.5" — all of these are now correctly rejected, while "123" works as expected. Also, non-numeric characters like "abc" can’t even be typed into the input field anymore, so they’re automatically prevented at the keyboard level.
This should now fully match the intended behavior.
debugging/book-library/script.js
Outdated
myLibrary.splice(i, 1); | ||
render(); | ||
// Delete button | ||
const delCell = row.insertCell(4); |
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.
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.
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.
Hi cjyuan,
Thanks for the feedback! I see the pros and cons:
- Keeping all cell creation code in one place makes the table structure clearer.
- Splitting it up makes the logic around buttons easier to manage but reduces readability.
To balance both, I’ll keep the cell creation in one centralized block but move the button setup (delete, toggle read) into small helper function. That way, the row structure stays easy to follow while keeping the button logic modular.
Your PR couldn't be matched to an assignment in this module. Please check its title is in the correct format, and that you only have one PR per assignment. If this PR is not coursework, please add the NotCoursework label (and message on Slack in #cyf-curriculum or it will probably not be noticed). |
debugging/book-library/script.js
Outdated
const table = document.getElementById("display"); | ||
const tbody = table.querySelector("tbody"); | ||
// clear old rows | ||
tbody.innerHTML = ""; | ||
table.innerHTML = ""; |
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.
Clearing the whole table would also remove the header row in the table.
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.
Hi cjyuan,
Thanks for pointing this out! I updated my code to only clear the instead of the entire table, so the header row is preserved while old book rows are removed.
debugging/book-library/script.js
Outdated
checkCell.textContent = book.check ? "Read" : "Not Read"; | ||
|
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.
What about the checkbox event handler?
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.
Hi cjyuan,
Thank you for your guidence.
I added an event handler for the Read/Not Read button, which toggles the check property on the correct book object and re-renders the table. This ensures the status updates immediately when clicked.
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.
debugging/book-library/script.js
Outdated
function createDeleteCell(cell, book) { | ||
const deleteBtn = document.createElement("button"); | ||
deleteBtn.textContent = "Delete"; | ||
deleteBtn.addEventListener("click", () => { | ||
const index = myLibrary.indexOf(book); | ||
if (index > -1){ | ||
myLibrary.splice(index, 1); | ||
render(); | ||
} | ||
}); | ||
cell.appendChild(deleteBtn); | ||
} | ||
|
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.
Why use book
instead of index
? Both work for this simple app, I am just curious about your rationale.
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.
Hi cjyuan,
Thank you for your assistance.
That’s a good question. I went with passing the book object instead of the index mainly for readability — it felt more natural to delete by reference since I already had access to the book when creating the cell.
I realize using index might be a bit more direct (and avoids calling indexOf), so I see the pros and cons of both approaches. For this small app both solutions work fine, but in a larger app with potential duplicates or performance concerns, using index would likely be the better option.
debugging/book-library/script.js
Outdated
const readBtn = document.createElement("button"); | ||
readBtn.textContent = book.check ? "Read" : "Not Read"; | ||
readBtn.className = book.check ? "btn btn-success" : "btn btn-secondary"; | ||
readBtn.addEventListener("click", () => { | ||
book.check = !book.check; // toggle | ||
render(); // re-render table | ||
}); | ||
checkCell.appendChild(readBtn); |
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.
Why implement a function for "delete button" but not doing the same consistently for the "check box"?
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.
Hi cjyuan,
Thanks for the feedback!
You’re absolutely right. I know how important refactoring code is. We should always break down our functions into smaller functions that each do a specific job. This method helps make the code clean, reusable, and easier to understand. I’m sorry—I think I was in a rush this time. My apologies.
I refactored the read/unread button logic into a helper function similar to the delete button.
Self checklist
Changelist
This pull request for syn my fork and updating my branch.
Questions
Please, Let me know if there is anything else went wrong?