Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Story {
- constructor(title, creator) {
- this.title = title
- this.creator = creator;
- this.comments = [];
- this.likes = [];
- this.replies = [];
- }
- get likes() {
- if (this._likes.length <= 0) {
- return `${this.title} has 0 likes`;
- } else if (this._likes.length === 1) {
- return `${this._likes} likes this story!`;
- } else {
- return `${this._likes[0]} and ${this._likes.length - 1} others like this story!`;
- }
- }
- set likes(value) {
- this._likes = value;
- }
- set comments(value) {
- this._comments = value;
- }
- like(username) {
- if (this.likes.includes(username)) {
- throw new Error("You can't like the same story twice!");
- }
- if (this.creator === username) {
- throw new Error("You can't like your own story!");
- }
- this._likes.push(username);
- return`${username} liked ${this.title}!` ;
- }
- dislike(username) {
- if (!this.likes.includes(username)) {
- throw new Error("You can't dislike this story!");
- }
- this._likes = this._likes.filter(e => e.username === username);
- return `${username} disliked ${this.title}`;
- }
- comment(username, content, id) {
- if (this._comments.includes(id)) {
- this.replies.push(id);
- return "You replied successfully";
- }else{
- this._comments.push({id, username, content, Replies: 0});
- return `${username} commented on ${this.title}`;
- }
- }
- toString(sortingType) {
- let res ='';
- if (sortingType === 'asc') {
- this._comments.sort((a,b) => a.id.localeCompare(b.id));
- this._comments.replies.sort((a,b) => a.id.localeCompare(b.id))
- }
- if (sortingType === 'desc') {
- this._comments.sort((a,b) => b.id.localeCompare(a.id));
- this._comments.replies.sort((a,b) => b.id.localeCompare(a.id))
- }
- res += `Title: ${this.title}\nCreator: ${this.creator}\nLikes: ${this._likes.length}\nComments:\n`
- return res;
- }
- }
Add Comment
Please, Sign In to add comment