Guest User

Untitled

a guest
Apr 19th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. class Media{
  2. constructor(title){
  3. this._title = title;
  4. this._isCheckedOut = false;
  5. this._ratings = [];
  6. }
  7. get title(){
  8. return this._title;
  9. }
  10. get isCheckedOut(){
  11. return this._isCheckedOut;
  12. }
  13. get ratings(){
  14. return this._ratings;
  15. }
  16. set isCheckedOut(checked){
  17. this._isCheckedOut = checked;
  18. }
  19. toggleCheckOutStatus(){
  20. this._isCheckedOut = !this._isCheckedOut;
  21. }
  22. getAverageRating(){
  23. let lengthOfArray = this._ratings.length;
  24. let result = this._ratings.reduce((accumulator, rating) => accumulator + rating, 0);
  25. return result/lengthOfArray;
  26.  
  27. }
  28. addRating(newRating){
  29. this._ratings.push(newRating);
  30. }
  31. }
  32. //const newMedia = new Media('Jumanji', false, [5, 4, 5, 3, 2, 1,6, 7]);
  33. //console.log(newMedia.getAverageRating());
  34. //console.log(newMedia.isCheckedOut);
  35.  
  36. class Book extends Media{
  37. constructor(title, author, pages){
  38. super(title);
  39. this._author = author;
  40. this._pages = pages;
  41. }
  42. get author(){
  43. return this._author;
  44. }
  45. get pages(){
  46. return this._pages;
  47. }
  48.  
  49. }
  50. const historyOfEverything = new Book('Bill Bryson', 'A Short History of Nearly Everything', 544);
  51. historyOfEverything.toggleCheckOutStatus();
  52. console.log(historyOfEverything.isCheckedOut);
  53. historyOfEverything.addRating(4);
  54. historyOfEverything.addRating(5);
  55. historyOfEverything.addRating(6);
  56. console.log(historyOfEverything.getAverageRating());
  57.  
  58. class Movie extends Media{
  59. constructor(title, director, runTime){
  60. super(title);
  61. this._director = director;
  62. this._runTime = runTime;
  63. }
  64. get director(){
  65. return this._director;
  66. }
  67. get runTime(){
  68. return this._runTime;
  69. }
  70. }
  71.  
  72. const speed = new Movie('Jan deBont', 'Speed', 116);
  73. speed.toggleCheckOutStatus();
  74. console.log(speed.isCheckedOut);
  75. speed._ratings = [4, 3, 4];
  76. //speed.addRating(1);
  77. //speed.addRating(2);
  78. //speed.addRating(3);
  79. console.log(speed.getAverageRating());
Add Comment
Please, Sign In to add comment