Advertisement
Guest User

Untitled

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