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 = [];
- }
- get _likes() {
- if (this.likes.length == 0) {
- return `${this.title} has 0 likes`;
- } else if (this.likes.length == 1) {
- return `${this.likes[0].join('')} likes this story!`
- } else {
- return `${this.likes[0]} and ${this.likes.length - 1} others like this story!`;
- }
- }
- like(username) {
- if (this.likes.includes(username)) {
- return `You can't like the same story twice!`;
- } else if (this.creator == username) {
- return "You can't like your own story!";
- } else {
- this.likes.push(username);
- return this.likes;
- }
- }
- dislike(username) {
- if (!this.likes.includes(username)) {
- return "You can't dislike this story!";
- } else {
- let index = this.likes.indexOf(username);
- this.likes.splice(index, 1);
- return `${username} disliked ${this.title}`;
- }
- }
- comment(username, content, id) {
- if (this.comments.hasOwnProperty(id) == false) {
- this.comments.push({ id, username, content, 'replies': {} })
- return `${this.comments.username} commented on ${this.title}`;
- } else if (this.comments.hasOwnProperty(id)) {
- let countReplies = 0;
- countReplies++;
- this.comments['replies'] = { id: `${id}.${countReplies}`, username, content };
- return "You replied successfully";
- }
- }
- toString(criteria) {
- let sortMap = {
- asc: (a, b) => a.id - b.id,
- desc: (a, b) => b.id - a.id,
- username: (a, b) => a.username.localeCompare(b.username)
- }
- let sortedComs = this.comments.sort(sortMap[criteria])
- let resultArr = [
- `Title: ${this.title}`,
- `Creator: ${this.creator}`,
- `Likes: ${this.likes.length}`,
- `Comments:${this.comments}`,
- ]
- return resultArr.join('\n');
- }
- }
- let art = new Story("My Story", "Anny");
- console.log(art)
- art.like("John");
- console.log(art.likes);
- art.dislike("John");
- console.log(art.likes);
- art.comment("Sammy", "Some Content");
- console.log(art.comment("Ammy", "New Content"));
- art.comment("Zane", "Reply", 1);
- art.comment("Jessy", "Nice :)");
- console.log(art.comment("SAmmy", "Reply@", 1));
- console.log()
- console.log(art.toString('username'));
- console.log()
- art.like("Zane");
- console.log(art.toString('desc'));
Advertisement
Add Comment
Please, Sign In to add comment