Advertisement
SavaIv

Untitled

Jun 21st, 2021
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. class Story {
  2.  
  3. constructor(title, creator) {
  4. this.title = title;
  5. this.creator = creator;
  6. this._comments = [];
  7. this._likes = [];
  8. this._counter = 1;
  9. }
  10.  
  11. get likes() {
  12. if (this._likes.length === 0) {
  13. return `${this.title} has 0 likes`;
  14. } else if (this._likes.length === 1) {
  15. return `${this._likes[0]} likes this story!`;
  16. } else {
  17. return `${this._likes[0]} and ${this._likes.length - 1} others like this story!`;
  18. }
  19. }
  20.  
  21. like(username) {
  22. if (this._likes.includes(username)) {
  23. throw new Error('You can\'t like the same story twice!');
  24. } else if (username === this.creator) {
  25. throw new Error('You can\'t like your own story!');
  26. } else {
  27. this._likes.push(username);
  28. return `${username} liked ${this.title}!`;
  29. }
  30. }
  31.  
  32. dislike(username) {
  33. if (!this._likes.includes(username)) {
  34. throw new Error('You can\'t dislike this story!')
  35. } else {
  36. let theIndex = this._likes.indexOf(username);
  37. this._likes.splice(theIndex, 1);
  38. return `${username} disliked ${this.title}`;
  39. }
  40. }
  41.  
  42. comment(username, content, id) {
  43.  
  44. let temp = this._comments.find(e => e.id === id);
  45.  
  46. if (temp === undefined || id === undefined) {
  47. let newTemp = {
  48. id: this._counter,
  49. username: username,
  50. content: content,
  51. replies: []
  52. }
  53. this._comments.push(newTemp);
  54. this._counter++;
  55. return `${username} commented on ${this.title}`;
  56.  
  57. } else {
  58.  
  59. let replayNumber = temp.replies.length + 1;
  60. let replayObject = {
  61. id: `${id}.${replayNumber}`,
  62. username: username,
  63. content: content
  64. }
  65. temp.replies.push(replayObject);
  66. return `You replied successfully`;
  67. }
  68. }
  69.  
  70. toString(sortingType) {
  71.  
  72. let sb = [];
  73.  
  74. let temp = `Title: ${this.title}`;
  75. sb.push(temp);
  76.  
  77. temp = `Creator: ${this.creator}`;
  78. sb.push(temp);
  79.  
  80. temp = `Likes: ${this._likes.length}`;
  81. sb.push(temp);
  82.  
  83. sb.push('Comments:');
  84.  
  85. // sortinng
  86. if (sortingType === 'asc') {
  87. this._comments.sort((a, b) => a.id - b.id);
  88. for (let i = 0; i < this._comments.length; i++){
  89. this._comments[i].replies.sort((a, b) => a.id - b.id);
  90. }
  91. } else if (sortingType === 'desc') {
  92. this._comments.sort((a, b) => (b.id) - (a.id));
  93. for (let i = 0; i < this._comments.length; i++){
  94. this._comments[i].replies.sort((a, b) => (b.id) - (a.id));
  95. }
  96. } else if (sortingType === 'username') {
  97. this._comments.sort((a, b) => a.username.localeCompare(b.username));
  98. for (let i = 0; i < this._comments.length; i++){
  99. this._comments[i].replies.sort((a, b) => a.username.localeCompare(b.username));
  100. }
  101. }
  102.  
  103. // sb.appendLine()
  104. this._comments.forEach(e => {
  105. temp = `-- ${e.id}. ${e.username}: ${e.content}`;
  106. sb.push(temp);
  107.  
  108. e.replies.forEach(i => {
  109. temp = `--- ${i.id}. ${i.username}: ${i.content}`;
  110. sb.push(temp);
  111. });
  112. });
  113.  
  114. return sb.join('\n');
  115. }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement