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…

2 Likes

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:

1 Like

agree on both of you

here i think it means.
that suddenly asking for catalog but not so much explanation about it.
that’s why many of us confused.
it was not clear what to make.
but anyway

thank you for nice advice! to become a junior developer it is really good to strengthen my own ability to think.

1 Like

Hi @willemijnbb8, @laurynb8604524326 and @tera8793863138,

I know this is an old thread but I thought it may help others in the future.

This can be done automatically from within the Media parent class, this is my thought process:

  1. First we need to create a Catalog class with a media array, a getter to retrieve the media array and a method to add to the media array.

  2. Now we need to initiate the catalog class by creating a new instance - NOTE: the Catalog class should be initiated BEFORE any of the Media, Book, Movie or CD instances are initiated because each time the Media constructor is called by a new instance, it will look for the addMedia() method from the Catalog class. To save issues we can initiate this class straight after the class it self as shown below.

  3. Now we can use the addMedia() method from the Catalog class within the Media class constructor.

// STEP 1: Catalog class. 
class Catalog {
    constructor() {
        this._media = [];
    }

    get media() {
        return this._media;
    }

    addMedia(media) {
        this._media.push(media);
    }
}

// STEP 2: New catalog instance.
const catalog = new Catalog();

// Media parent class.
class Media {
    constructor(title) {
        this._title = title;
        this._isCheckedOut = false;
        this._rating = [];

        // STEP 3: Add all media to catalog
        catalog.addMedia(this);
    }

    ...

}

// Book subclass.
class Book extends Media {
    ...
}

// Movie subclass.
class Movie extends Media {
    ...
}

// CD subclass.
class CD extends Media {
    ...
}

So now we are all set up, we can start creating new instances of Book, Movie and CD which will all be added to our catalog.

// Book instance
const historyOfEverything = new Book('A Short History of Nearly Everything', 'Bill Bryson', 544);

// Movie instance
const speed = new Movie('Speed', 'Jan de Bont', 116);

// CD instance
const allEyesOnMe = new CD('All Eye\'s On Me', 'Tupac Shakur', 116);

Now lets view our new catalog:

// View the catalog in the console
console.log(catalog.media);

This shows us an Array of Objects something similar to this:

[
  Book {
    _title: 'A Short History of Nearly Everything',
    _isCheckedOut: true,
    _rating: [ 4, 5, 5 ],
    _author: 'Bill Bryson',
    _pages: 544
  },
  Movie {
    _title: 'Speed',
    _isCheckedOut: true,
    _rating: [ 1, 1, 5 ],
    _director: 'Jan de Bont',
    _runTime: 116,
    _movieCast: [ 'Kevin Hart', 'Denzil Washington', 'Sandra Bullock' ]
  },
  CD {
    _title: "All Eye's On Me",
    _isCheckedOut: false,
    _rating: [ 3, 2, 5 ],
    _artist: 'Tupac Shakur',
    _songs: [
      'Dear Mama',
      'Picture Me Rolling'
    ]
  }
]

It took me some time to understand. It would of been a whole lot easier and saved a lot of time if someone would of explained the purpose of this in the first place. So I really hope this helps you and anyone else who also may get stuck with this task.

Kind Regards
Wesley

2 Likes