Build library - Step 25 Create class called Catalog that holds all of the Media items in our library

hi all ,
any ideas on how to make my code a bit smaller if possible , and also is there away of combining the add methods into one
and also the same goes for the remove and the find methods?? thanks
here is my code :

// Catalog that holds all of the Media items in library

let catalog = {

  _books: [],

  _movies:[],

  _cds:[],

  get books(){

      return this._books;

  },

  get Movies(){

      return this._movies;

  },

  get cds(){

      return this._cds;

  },

  addBook(book){

    this._books.push(book)

  },

  addMovies(movie){

    this._movies.push(movie)

  },

  addCds(cd){

      this._cds.push(cd)

  },

  removeBook(book){

    let booksaArr = this._books;

    for(let i = 0; i < booksaArr.length; i++){

      if(booksaArr[i].title === book){

       delete booksaArr[i]

      }

    }

   },

   removeMovie(movie){

     let movieArr = this._movies;

     for(let i = 0; i < movieArr.length; i++){

       if(movieArr[i].title === movie){

         delete movieArr[i]

       }

     }

   },

   removeCd(cd){

     let cdArr = this._cds;

     for(let i = 0; i < cdArr.length; i++){

       if(cdArr[i].title === cd){

         delete cdArr[i]

       }

     }

   },

  findBook(book){

    let booksaArr = this._books;

    for(let i = 0; i < booksaArr.length; i++){

      if(booksaArr[i].title === book){

        let Book = booksaArr[i]

        console.log(`(Title: ${Book.title}),(Author: ${Book.author}),(ratings: ${Book.ratings}),(Average rating: ${Book.averageRating}),(Pages: ${Book.pages}),(Checked Out: ${Book.isCheckedOut})`)

      }

    }

   },

   findMovie(movie){

     let movieArr = this._movies;

     for(let i = 0; i < movieArr.length; i++){

       if(movieArr[i].title === movie){

         let Movie = movieArr[i]

         console.log(`(Title: ${Movie.title}),(Director: ${Movie.director}),(ratings: ${Movie.ratings}),(Average rating: ${Movie.averageRating}),(Run Time: ${Movie.runTime}),(Checked Out: ${Movie.isCheckedOut})`)

       }

     }

   },

   findCd(cd){

     let cdArr = this._cds;

     for(let i = 0; i < cdArr.length; i++){

       if(cdArr[i].title === cd){

         let Cd = cdArr[i]

         console.log(`(Title: ${Cd.title}),(Artist: ${Cd.artist}),(ratings: ${Cd.ratings}),(Average rating: ${Cd.averageRating}),(Tracks: ${Cd.songs}),(Shuffled Tracks: ${Cd.shuffledTracks}),(Checked Out: ${Cd.isCheckedOut})`)

       }

     }

   },

}

1 Like

Hi,
if you used the filter method rather than a for loop + delete for the removal methods, you get a more concise code. Also, the delete operator doesn’t seem fit for this task to me as it is supposed to remove object properties while you are applying it for arrays. After deleting an item, it won’t be gone completely, but there will still be ‘undefined’ left…

Yes, you could pass a second parameter for the medium to the method and write just one method for deleting and one for adding an item to whatever medium (book, cd etc.).

Summary
const catalog = {
...
  removeItem(title, medium) {
    this[medium] = this[medium].filter(m =>m.title !== title)
  }
}
catalog.removeItem('Ok Computer', 'cds')

You need a getter and setter (or object key) called ‘cds’ if you’d be writing it like this.

1 Like

thank you so much that definitely makes a lot more sense!

1 Like