Build a Library extra Catalog challenge

I finished the required tasks in the Build a Library project and most of the extra challenges (albeit with a little googling), but I can’t figure out how to do the very last one:
" Create class called Catalog that holds all of the Media items in our library."
I did manage to find some guidance as to how to create a variable to hold all the items, but not a class. It doesn’t really make sense to me either to use a class rather than a variable (array of objects) for this, since we’re not ‘classifying’ the objects here but just storing data. Can anyone explain the technique and the reasoning behind this?

https://www.codecademy.com/paths/web-development/tracks/webdev-intermediate-javascript/modules/learn-javascript-classes/projects/build-a-library

1 Like

Once we add the Catalog class, we could start classifying objects. Or we could implement search methods in the Catalog class.

the catalog class would then hold all the Media items in an array or object.

Thanks for the quick reply! I’m not sure I get it though… What would the syntax for this class be?

First you need to determine what this class does, then express that behavior in syntax.

The basic functionality of this class will be very similar to what you already with a variable to hold all the items.

Actually I’m not even sure what it’s supposed to do exactly… The same challenge is at the end of the next project (https://www.codecademy.com/paths/web-development/tracks/webdev-intermediate-javascript/modules/learn-javascript-classes/projects/school-catalog).

I’m guessing that this class would hold an array of objects (each object in the array being an instance, in the media project a cd/book/movie, in the school project a school).
I’m trying to reason how to do that in variable syntax first, but I can’t find any methods on how to access items of a particular class. I’m not sure how to google it either as most of my search queries just lead me to DOM methods…
And then once I figure out that method, I wouldn’t know how to do it in class syntax, haha. It would be great if you could show me the proper syntax and I’ll analyze its workings for myself. :slight_smile:

albeit with a little googling

the more the better. though, when someone’s suggesting how to solve a problem watch out, and think for yourself. this can be particularly bad for javascript because of popularity/accessibility/permissive language

If what you’re looking for is about a language feature then you’ll want to look for results on this site:

Sorry for this late reply. I did look over the Classes page on MDN a couple of times, but still can’t find how to access all instances of a particular class.
And then referring back to my earlier reply :wink: - what the syntax is for creating this Catalog class to hold all Media items.

before you can do that you have to make sure to remember them all somewhere … you’re going to have many of something, so that would be an array, and if it’s supposed to have all instances then you’d need to add an instance to that array as soon as you create the instance

if a class should hold many of something… then first of all you would need a class, so you might create a class with nothing in it, maybe even test that you can create instances of it before you start filling it in. then, to hold many of something… that’s an array, and if the class is supposed to hold the many things then the array should be an attribute of the class

Ok, that makes sense. So there’s no default way in JS to access instances of a class? You can only do that manually?

Again :stuck_out_tongue:, if anyone has the syntax ready to solve this problem, that would help a lot because then I can look over it and understand it properly.

i suppose you could use objects:

https://developer.mozilla.org/nl/docs/Web/JavaScript/Reference/Global_Objects/Object

these have some of the method (defineProperty, hasOwnProperty) which might be useful. You can also loop over objects. So its an alternative to arrays, although i would use arrays. ionatan can explain this better then i can :wink:

i don’t agree, then you are missing the fundamental step in problem solving. Programming is foremost problem solving.

I agree with that generally and would always try to solve it myself before resorting to just the answer, but I also know myself well enough to know that I do learn very well from analyzing a given answer and seeing how the syntax works :wink: In all honesty after 4 times of saying this it’s getting a little frustrating. I’ve made it quite clear that I don’t understand it now from the information given so far (although that’s appreciated of course, thanks!) and that the syntax itself would help me understand…

1 Like

but there is nothing new here? We can just create a class to hold all our items:

class Catalog {
  constructor(){
    this.items = [];
  }
}

// rest of the classes

const catalog = new Catalog();

Hi @willemijnbb8, agreed!

I am still working out this issue but so far I’ve figured out how to make a catalog variable containing all instances of the Media parent class.

I created a catalog variable outside of the classes like this:

let catalog = [];

Then I added a line at the bottom of my parent class that will automatically push all new instances of the Media class to the catalog variable. It looks like this:

class Media {
  constructor(title) {
    this._title =  title;
    this._ratings = [];
    this._isCheckedOut = false;
    catalog.push(this);
  }
};

When I logged my catalog variable to the console it output all of the instances of Media that I created. I’ll post an update once I figure out how to make the catalog variable a class.

Not sure if this is helpful to you at all but figured I would share! :slight_smile:

2 Likes

THANK YOU! That definitely helps so much and it’s really clear - actually in hindsight I can’t believe I didn’t get this far, haha, as it’s actually so easy… at least for creating a variable and automatically pushing items from a class into the array.
Now to find out how to create a class for holding the items as is asked in the exercise… but tbh I’m starting to think the exercise is just phrased weirdly and this is what they want us to do. It doesn’t really make sense to me either since as far as I know a class isn’t really supposed to hold data the way a variable would? Ah well.
Thanks again :slight_smile:

The Catalog class acts as a sort of wrapper, to the Catalog class we could now add a search method for example.

Agree about not understanding the real world use. If classes are templates for objects then I’m not sure why we would want a catalog class in this specific scenario so maybe I’ll just move on from this one. But testing out the push method to the variable helped me too!

I wish they gave the solutions (or hints at least) to the extra parts of projects because like you mentioned above, I learn a lot from seeing examples. As new coders, I don’t think that’s a reflection of our problem solving abilities :upside_down_face:

I agree with you here :slight_smile: Understanding why we would use a class here isn’t explained. So that really should be the question. The exercise doesn’t seem to explain the why

if i had to guess, it would be because now we have all our data in one class, allowing us to write a search method for example, which allows use to search through our entire catalog/inventory.

I don’t think there is a solution. Its depended on what you want to achieve, which is now in your hands :slight_smile:

Interesting. Lets say you land a junior developer job. Your assignment now is that the application need some new functionality/feature. Of course it will be a small feature first.

how do you get from viewing code examples to solving the problems yourself as will happen in a junior developer job? :slight_smile: