Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2020
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. class Article{
  2. _comments= [];
  3. _likes = [];
  4. constructor(title, creator){
  5. this.title = title;
  6. this.creator = creator;
  7. }
  8. get likes(){
  9. if (this._likes.length === 0) {
  10. return `${this.title} has 0 likes`
  11. }
  12. if (this._likes.length === 1) {
  13. return `${this._likes[0]} likes this article!`
  14. }
  15. return `${this._likes[0]} and ${this._likes.length - 1} others like this article!`
  16. }
  17. like (username){
  18. if (this._likes.some(x=> x === username)) {
  19. throw new Error("You can't like the same article twice!");
  20. }
  21. if (this.creator === username) {
  22. throw new Error("You can't like your own articles!");
  23. }
  24. this._likes.push(username);
  25. return `${username} liked ${this.title}!`
  26. }
  27. dislike (username){
  28. if (this._likes.every(x=> x !== username)) {
  29. throw new Error(`You can't dislike this article!"`);
  30. }
  31. this._likes = this._likes.filter(x=> x!== username)
  32. return `${username} disliked ${this.title}`
  33. }
  34. comment (username, content, id){
  35. let targetComment = this._comments.find(x=> x.Id === id)
  36. if (id === undefined || !targetComment) {
  37. let nextID = this._comments.length+1;
  38. let newestComment = {Id: nextID, Username: username, Content: content, Replies: []}
  39. this._comments.push(newestComment);
  40. return `${username} commented on ${this.title}`;
  41. }
  42. if (targetComment) {
  43. let repliesCount = targetComment.Replies.length+1;
  44. let currReply = `${targetComment.Id}.${repliesCount}`
  45. let newestReply = {Id: currReply, Username: username, Content: content}
  46. targetComment.Replies.push(newestReply);
  47. return `You replied successfully`
  48. }
  49. }
  50. toString(sortingType){
  51. let result = '';
  52. result += `Title: ${this.title}\nCreator: ${this.creator}\nLikes: ${this._likes.length}\nComments:\n`
  53. if (sortingType === 'asc') {
  54. this._comments = this._comments.sort((a,b)=> a.Id-b.Id);
  55. for (const com of this._comments) {
  56. result+=`-- ${com.Id}. ${com.Username}: ${com.Content}\n`
  57. com.Replies = com.Replies.sort((a,b)=> +(a.Id)-(+(b.Id)));
  58. for (const reply of com.Replies) {
  59. result += `--- ${reply.Id}. ${reply.Username}: ${reply.Content}\n`
  60. }
  61. }
  62. }
  63. else if(sortingType === 'desc'){
  64. this._comments = this._comments.sort((a,b)=> b.Id-a.Id);
  65. for (const com of this._comments) {
  66. result+=`-- ${com.Id}. ${com.Username}: ${com.Content}\n`
  67. com.Replies = com.Replies.sort((a,b)=> +(b.Id)-(+(a.Id)));
  68. for (const reply of com.Replies) {
  69. result += `--- ${reply.Id}. ${reply.Username}: ${reply.Content}\n`
  70. }
  71. }
  72. }else{
  73. this._comments = this._comments.sort((a,b)=> a.Username.localeCompare(b.Username))
  74. for (const com of this._comments) {
  75. result+=`-- ${com.Id}. ${com.Username}: ${com.Content}\n`
  76. com.Replies = com.Replies.sort((a,b)=> a.Username.localeCompare(b.Username));
  77. for (const reply of com.Replies) {
  78. result += `--- ${reply.Id}. ${reply.Username}: ${reply.Content}\n`
  79. }
  80. }
  81. }
  82.  
  83. return result.trim();
  84. }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement