I'm having trouble with a library project

So I'm doing a library project and I've been tackling it step by step. All the apps need to do is create book cards from user input and display them in a grid but I haven't been able to add them to my array. Here's the codepen: https://codepen.io/norwyx/pen/rNbZOmE?editors=0011 A little bit of context: all the functions at the bottom are pure UI stuff so not that important. But at the top I have things like the Book object that has some default values inside the constructor and then the properties. The Library object with the books constructor (the array Im having problems with) and the helper functions suck as addBook, getBook, inLibrary, and removeBook. But what I'm facing is that I'm able to correctly get the info from the form, and successfully create the object (check the console, I left a console.log). But then when I need to push it into the library.books array, it is not working and I'm not storing the object anywhere. Help would be very appreciated since I've been trying stuff for 2 days and no shot, so now I need some help. Thanks in advance.
50 Replies
ἔρως
ἔρως8mo ago
you're using the wrong tool for the job instead of an array, use a map then map the book objects to the book name an object could work too
MarkBoots
MarkBoots8mo ago
also, it didnt work because you do a filter on the books array to see if it exsist. if it not exist it will return an empty array, that is still considered truthy
ἔρως
ἔρως8mo ago
yeah, an array is truthy or falsy depends on how you check the way you're doing, it is always truthy
MarkBoots
MarkBoots8mo ago
the array.find method would work, but indeed. a map would be even better
ἔρως
ἔρως8mo ago
yes, it is a tiny bit more annoying to use, but makes things a lot easier
MarkBoots
MarkBoots8mo ago
adjusted to use a map (with title as key)
class Book {
constructor (
title = 'Unknown',
author = 'Unknown',
pages = 0,
isRead = false
) {
this.title = title
this.author = author
this.pages = pages
this.isRead = isRead
}
}

class Library {
constructor () {
this.books = new Map();
}
addBook (newBook) {
if (!this.inLibrary(newBook)) {
this.books.set(newBook.title, newBook);
}
}
getBook (title) {
return this.books.get(title);
}
inLibrary (newBook) {
return this.books.has(newBook.title);
}
removeBook (title) {
return this.books.delete(title);
}
}
class Book {
constructor (
title = 'Unknown',
author = 'Unknown',
pages = 0,
isRead = false
) {
this.title = title
this.author = author
this.pages = pages
this.isRead = isRead
}
}

class Library {
constructor () {
this.books = new Map();
}
addBook (newBook) {
if (!this.inLibrary(newBook)) {
this.books.set(newBook.title, newBook);
}
}
getBook (title) {
return this.books.get(title);
}
inLibrary (newBook) {
return this.books.has(newBook.title);
}
removeBook (title) {
return this.books.delete(title);
}
}
ἔρως
ἔρως8mo ago
this.books = this.books.delete(title); <-- delete updates the map in place that line is wrong https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/delete#return_value you're setting this.books to a boolean instead, do return this.books.delete(title); i would even use a private value just add #books = new Map(); and you're set and all references to this.books become this.#books
MarkBoots
MarkBoots8mo ago
oops, you were right
ἔρως
ἔρως8mo ago
were? 😲 i am right 🤣 but yeah, the class looks clean af, super easy to read and not error prone
MarkBoots
MarkBoots8mo ago
haha, yea. i wont overkill with too much class options for now. book could even extend the library class
ἔρως
ἔρως8mo ago
nah, this is fine at is it
MarkBoots
MarkBoots8mo ago
think so too
ἔρως
ἔρως8mo ago
i think so too
Norwyx
NorwyxOP8mo ago
So I've never seen Maps. Now I can't seem to display the information I get. I'm able to see the info from the form but I can't seem to access where the books are stored. Why is this line not working?
console.log([...library.books.entries()])
console.log([...library.books.entries()])
Please keep in mind I have never seen Maps
ἔρως
ἔρως8mo ago
show us the updated codepen
Norwyx
NorwyxOP8mo ago
Sure
ἔρως
ἔρως8mo ago
that still uses an array
Norwyx
NorwyxOP8mo ago
Nah i switched it for a map
Norwyx
NorwyxOP8mo ago
No description
Norwyx
NorwyxOP8mo ago
maybe thats why? I fixed it, no longer that error but still can't find the library with the books inside
ἔρως
ἔρως8mo ago
const addBook = (e) => {
e.preventDefault()
const newBook = getBookFromInput
library.addBook(newBook)
console.log(newBook())
console.log([...library.books.entries()])
}
const addBook = (e) => {
e.preventDefault()
const newBook = getBookFromInput
library.addBook(newBook)
console.log(newBook())
console.log([...library.books.entries()])
}
this is the issue you're adding a function
Norwyx
NorwyxOP8mo ago
In the last line?
ἔρως
ἔρως8mo ago
no
Norwyx
NorwyxOP8mo ago
Tbh I just googled how to access info from a map and copy pasted SO
ἔρως
ἔρως8mo ago
library.addBook(newBook) <-- here
Norwyx
NorwyxOP8mo ago
fucking hell How didn't i read that thats crazy
ἔρως
ἔρως8mo ago
const newBook = getBookFromInput <-- no parenthesys, which means that you are setting newBook to be a function
Norwyx
NorwyxOP8mo ago
Fuck, i tought that when creating the functions the way i created them wasnt necessary to do it Pretty dumb from me i guess
ἔρως
ἔρως8mo ago
you just didn't run the function no running = no new book
Norwyx
NorwyxOP8mo ago
I just saw that way of creating functions when learning es6 and though it looked cleaner so the parenthesis should fix it? what about seeing stuff from the map is that line correct? console.log([...library.books.entries()])
ἔρως
ἔρως8mo ago
yes, it is
Norwyx
NorwyxOP8mo ago
ok let me try this
ἔρως
ἔρως8mo ago
it's not the best way, but it is correct
Norwyx
NorwyxOP8mo ago
What would be a more accurate way to do it? because it works but what would you do? trying to learn best practices
ἔρως
ἔρως8mo ago
for(let [title, book] of library.books.entries()) { .... } it's not a matter of "best practices", but a matter of not creating an useless array
Norwyx
NorwyxOP8mo ago
Btw look at how it is displaying
No description
ἔρως
ἔρως8mo ago
seems about right?
Norwyx
NorwyxOP8mo ago
Yeah yeah just showing you About what you said i removed the [] and now it shows the following way
Norwyx
NorwyxOP8mo ago
That means now Im not creating a useless array?
No description
ἔρως
ἔρως8mo ago
if you don't need the key, then that is fine
Norwyx
NorwyxOP8mo ago
Ok seems about right bro. Thank you so much and also thanks to Mark. I truly learnt a lot by just seeing you two talking Have a good night Or morning wherever you are 🙂
ἔρως
ἔρως8mo ago
it's way way into the night here
MarkBoots
MarkBoots8mo ago
Go to bed @ἔρως i'm already up
ἔρως
ἔρως8mo ago
i just woke up :/
MarkBoots
MarkBoots8mo ago
then we have the same schedule 😉
ἔρως
ἔρως8mo ago
im trying to fall back asleep
MarkBoots
MarkBoots8mo ago
good luck with that. i give it another 30 minutes to try so too, otherwise I'm making my morning coffee
ἔρως
ἔρως8mo ago
too early for morning
Want results from more Discord servers?
Add your server