London | 26-ITP-Jan | Abdul Moiz | Sprint 2 | Book Library#385
London | 26-ITP-Jan | Abdul Moiz | Sprint 2 | Book Library#385A-Moiz wants to merge 2 commits intoCodeYourFuture:mainfrom
Conversation
ykamal
left a comment
There was a problem hiding this comment.
Nice work! 👍 Things look mostly right. Well done. There are a few things that need a bit of work. I've left comments below. Let me know if you have any questions.
debugging/book-library/script.js
Outdated
| function populateStorage() { | ||
| if (myLibrary.length == 0) { | ||
| if (myLibrary.length === 0) { | ||
| let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true); |
There was a problem hiding this comment.
Love to see some OOP code. Since the class you're referencing here is declared further down the file, for the sake of readability and code organisation, I recommend moving the class definition to much higher, or to the beginning of this file.
debugging/book-library/script.js
Outdated
| function populateStorage() { | ||
| if (myLibrary.length == 0) { | ||
| if (myLibrary.length === 0) { | ||
| let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true); |
There was a problem hiding this comment.
book1 is not getting reassigned later, so using a const keyword will make it safer in the same scope. It's still going to allow you to run object mutations where applicable. but it ensures that you cannot redeclare a variable with the same name inside the same scope.
Same for book2
| pages.value == null || | ||
| pages.value == "" | ||
| ) { | ||
| if (title.value === "" || author.value === "" || pages.value === "") { |
There was a problem hiding this comment.
Good validation check 👍
However, if you do not remove unnecessary whitespaces from the start and the end, " " will pass through this check as it's a bunch of valid characters and is not an empty string. How do you think you could counter this?
debugging/book-library/script.js
Outdated
| let book = new Book( | ||
| title.value, | ||
| author.value, | ||
| String(pages.value), |
There was a problem hiding this comment.
This is good safe practice, but there is most likely no need to convert this into a string, as all input values are always returned (usually) as strings, unless something like this is used: https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/valueAsNumber
debugging/book-library/script.js
Outdated
| this.title = title; | ||
| this.author = author; | ||
| this.pages = pages; | ||
| this.check = check; |
There was a problem hiding this comment.
Since this is JavaScript, do you think we should ensure that these constructor arguments are provided in the right data type?
debugging/book-library/script.js
Outdated
There was a problem hiding this comment.
render() appears to be called twice on page load. Perhaps you should create a sequence of this from the root function call and not inside here?
| } else { | ||
| readStatus = "Yes"; | ||
| } | ||
| changeBut.innerText = readStatus; |
There was a problem hiding this comment.
Have you come across Ternary Operator? Could make the code cleaner:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_operator
debugging/book-library/script.js
Outdated
| delBut.className = "btn btn-warning"; | ||
| delBut.innerHTML = "Delete"; | ||
| delBut.addEventListener("clicks", function () { | ||
| delButton.id = i + 5; |
There was a problem hiding this comment.
I can see that you are trying to avoid duplicate IDs. Nice approach. However, the approach is hard-coding a specific number, and the outcome could be inconsistent.
You could just use delButton.id = "delButton_" + i or such tactics. That'll make them truly unique. Obviously, use string interpolation with backticks. I can't write that here.
debugging/book-library/script.js
Outdated
| delButton.id = i + 5; | ||
| deleteCell.appendChild(delButton); | ||
| delButton.className = "btn btn-warning"; | ||
| delButton.innerHTML = "Delete"; |
There was a problem hiding this comment.
use textContent as it parses the provided value as a string, whereas innerHTML parses it as HTML and should only be used if the change contains actual HTML. You can also use innerText for static text. textContent may be faster for most things.
| deleteCell.appendChild(delButton); | ||
| delButton.className = "btn btn-warning"; | ||
| delButton.innerHTML = "Delete"; | ||
| delButton.addEventListener("click", function (e) { |
There was a problem hiding this comment.
a better way to do this would be using data- attributes. You're using i inside a closure which, while fine, is not the best practice.
jQuery has excellent APIs for using data attributes. Even with core JavaScript, if you can master data attributes, you will no longer need to do these, and can use HTML itself for storing that reference.
https://developer.mozilla.org/en-US/docs/Web/HTML/How_to/Use_data_attributes
|
Hi @ykamal , I've made all the changes you requested. Let me know if you anymore recommendations. |
Learners, PR Template
Self checklist
Changelist
Questions
N/A