How to build a library

Dear all,

Could anyone check if my code is correct? I have a hard time figuring out how to drive addRatings and getAverageRating to work with the final execution of the entire block of code.

class Media { constructor(title) { this._title = title; this._isCheckedOut = false; this._ratings = []; } get title() { return this._title; } get isCheckedOut() { return this._isCheckedOut; } get ratings() { return this._ratings; } set isCheckedOut(value) { this._isCheckedOut = value; } toggleCheckedOutStatus() { this.isCheckedOut = this._isCheckedOut; } getAverageRating() { let ratingSum = this.ratings.reduce((accumulator, rating) => accumulator + rating); return ratingSum / this.ratings.lenght; } addRating(newRating) { this.ratings.push(newRating); if (newRating >= 1 && newRating <= 5) { console.log('Thanks for rating!'); } else { console.log('Try again. Your rate has to be between 1 to 5'); } } } class Book extends Media { constructor(author, title, pages) { super(title); this._author = author; this._pages = pages; } get author() { return this._author; } get pages() { return this._pages; } } class Movie extends Media { constructor(director, title, runTime) { super(title); this._director = director; this._runTime = runTime; } get director() { return this._director; } get runTime() { return this._runTime; } } const historyOfEverything = new Book('Bill Bryson', 'A Short Story Of Nearly Everything', 544); historyOfEverything.toggleCheckedOutStatus(); console.log(historyOfEverything.isCheckedOut); historyOfEverything.addRating(4); historyOfEverything.addRating(5); historyOfEverything.addRating(3); console.log(historyOfEverything.getAverageRating()); const speed = new Movie('Jan de Bont', 'Speed', 116); speed.toggleCheckedOutStatus(); console.log(speed.isCheckedOut); speed.addRating(2); speed.addRating(4); speed.addRating(1); console.log(speed.getAverageRating());

Line 24 has a typo (‘lenght’).

i should’ve known. is there anything i can do to make this better?

Better than what? Is the code neat and fluent? Does it all work? Is there any superfluous code? Can any of it be simplified and made easier to read? Can you explain each part of the code, what each class represents and how it relates to the others?

Better begins with understanding and purpose or goal. When we have a task to do the first order of business is understanding the problem and objectives. That would be where to start. Ensure that when you walk away from this project you are satisfied with your own level of understanding and comprehension, as well as with the execution of your plan. Code that works is our initial goal. Then ask all those earlier questions to determine if improvement is needed, or even possible.

2 Likes

Hi @denzeljoysugayan ,

just trying to solve that tasks myself… it seems to me that your code misses that ! for negation in the toggleCheckedOutStatus Function.
In the addRating Function i think it should be this._ratings.push(newRating) instead of this.ratings.push(newRating)

1 Like