SKGreyHound

02

Feb 20th, 2021
669
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. class Story {
  2. constructor(title, creator) {
  3. this.title = title;
  4. this.creator = creator;
  5. this.comments = [];
  6. this.likes = [];
  7. }
  8. get _likes() {
  9. if (this.likes.length == 0) {
  10. return `${this.title} has 0 likes`;
  11. } else if (this.likes.length == 1) {
  12. return `${this.likes[0].join('')} likes this story!`
  13. } else {
  14. return `${this.likes[0]} and ${this.likes.length - 1} others like this story!`;
  15. }
  16. }
  17. like(username) {
  18. if (this.likes.includes(username)) {
  19. return `You can't like the same story twice!`;
  20. } else if (this.creator == username) {
  21. return "You can't like your own story!";
  22. } else {
  23. this.likes.push(username);
  24. return this.likes;
  25. }
  26. }
  27. dislike(username) {
  28. if (!this.likes.includes(username)) {
  29. return "You can't dislike this story!";
  30. } else {
  31. let index = this.likes.indexOf(username);
  32. this.likes.splice(index, 1);
  33. return `${username} disliked ${this.title}`;
  34. }
  35. }
  36. comment(username, content, id) {
  37. if (this.comments.hasOwnProperty(id) == false) {
  38. this.comments.push({ id, username, content, 'replies': {} })
  39. return `${this.comments.username} commented on ${this.title}`;
  40. } else if (this.comments.hasOwnProperty(id)) {
  41. let countReplies = 0;
  42. countReplies++;
  43. this.comments['replies'] = { id: `${id}.${countReplies}`, username, content };
  44. return "You replied successfully";
  45. }
  46. }
  47. toString(criteria) {
  48. let sortMap = {
  49. asc: (a, b) => a.id - b.id,
  50. desc: (a, b) => b.id - a.id,
  51. username: (a, b) => a.username.localeCompare(b.username)
  52. }
  53. let sortedComs = this.comments.sort(sortMap[criteria])
  54. let resultArr = [
  55. `Title: ${this.title}`,
  56. `Creator: ${this.creator}`,
  57. `Likes: ${this.likes.length}`,
  58. `Comments:${this.comments}`,
  59. ]
  60. return resultArr.join('\n');
  61. }
  62. }
  63. let art = new Story("My Story", "Anny");
  64. console.log(art)
  65. art.like("John");
  66. console.log(art.likes);
  67. art.dislike("John");
  68. console.log(art.likes);
  69. art.comment("Sammy", "Some Content");
  70. console.log(art.comment("Ammy", "New Content"));
  71. art.comment("Zane", "Reply", 1);
  72. art.comment("Jessy", "Nice :)");
  73. console.log(art.comment("SAmmy", "Reply@", 1));
  74. console.log()
  75. console.log(art.toString('username'));
  76. console.log()
  77. art.like("Zane");
  78. console.log(art.toString('desc'));
Advertisement
Add Comment
Please, Sign In to add comment