Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Article {
- 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]} likes this article!`;
- } else {
- return `${this._likes[0]} and ${this._likes.length - 1} others like this article!`;
- }
- }
- like(username) {
- if (this._likes.includes(username)) {
- throw new Error("You can't like the same article twice!");
- }
- if (this.creator === username) {
- throw new Error("You can't like your own articles!");
- }
- this._likes.push(username);
- return `${username} liked ${this.title}!`;
- }
- dislike (username) {
- if (!this._likes.includes(username)) {
- throw new Error("You can't dislike this article!");
- } else {
- let index = this._likes.indexOf(username);
- this._likes.splice(index, 1);
- return `${username} disliked ${this.title}`;
- }
- }
- comment (username, content, id) {
- let obj = {
- username: username,
- content: content,
- id: id,
- replies: []
- };
- let comment = this._comments.filter(x => x.id === id)[0];
- if (obj.id === undefined || comment === undefined) {
- obj.id = this._comments.length + 1;
- this._comments.push(obj);
- return `${username} commented on ${this.title}`;
- } else {
- let reply = {
- username: username,
- content: content,
- id: id += comment.replies.length * 0.1 + 0.1
- };
- comment.replies.push(reply);
- return `You replied successfully`;
- }
- }
- toString(sortingType) {
- if (sortingType === 'asc') {
- this._comments.sort((a, b) => a.id - b.id);
- for (let i = 0; i < this._comments.length; i++) {
- const replies = this._comments[i].replies;
- replies.sort((a, b) => a.id - b.id);
- }
- } else if (sortingType === 'desc') {
- this._comments.sort((a, b) => b.id - a.id);
- for (let i = 0; i < this._comments.length; i++) {
- const replies = this._comments[i].replies;
- replies.sort((a, b) => b.id - a.id);
- }
- } else if (sortingType === 'username') {
- this._comments.sort((a, b) => a.username.localeCompare(b.username));
- for (let i = 0; i < this._comments.length; i++) {
- const replies = this._comments[i].replies;
- replies.sort((a, b) => a.username.localeCompare(b.username));
- }
- }
- let output = '';
- output += `Title: ${this.title}\n`;
- output += `Creator: ${this.creator}\n`;
- output += `Likes: ${this._likes.length}\n`;
- output += 'Comments:\n';
- for (let i = 0; i < this._comments.length; i++) {
- const comment = this._comments[i];
- if (comment.replies.length > 0) {
- output += `-- ${comment.id}. ${comment.username}: ${comment.content}\n`;
- for (let j = 0; j < comment.replies.length; j++) {
- const reply = comment.replies[j];
- output += `--- ${reply.id}. ${reply.username}: ${reply.content}\n`;
- }
- } else {
- output += `-- ${comment.id}. ${comment.username}: ${comment.content}\n`;
- }
- }
- return output.trim();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement