Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Story {
- #likes=[];
- constructor (title, creator){
- this.title=title;
- this.creator=creator;
- this.comments=[];
- this.currentCommentId=0;
- this.likesArr=[];
- }
- like(username){
- if (this.#likes.includes(username)){
- console.log('You cant like the same story twice!');
- 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!"`)
- }
- let userExists=false;
- for (let i=0; i<this.#likes.length;i++){
- let usr=this.#likes[i];
- if (username==usr){
- userExists=true;
- }
- }
- if (!userExists){
- console.log('hello, why you not working');
- console.log(this.#likes.length)
- this.#likes.push(username);
- console.log(this.#likes.length);
- return `${username} liked ${this.title}!`
- }
- }
- 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 story!`
- } else if (this.#likes.length>1){
- return `${this.#likes[0]} and ${this.#likes.length-1} others like this story!`
- }
- }
- dislike(username){
- //decrease dislikes once I figure em out
- if (this.#likes.includes(username)==false){
- throw new Error (`You can't dislike this story!`);
- } else {
- this.#likes.splice(this.#likes.indexOf(username));
- return `${username} disliked ${this.title}`
- }
- }
- comment(username, content, id){
- let existingComment=this.comments.find(c=>c.id==id);
- if (existingComment==undefined){
- this.currentCommentId++;
- let newComment=
- {id:this.currentCommentId,
- username:username, content:content,replies:[]
- }
- this.comments.push(newComment);
- return `${username} commented on ${this.title}`
- } else {
- let replyId=`${existingComment.id}.${existingComment.replies.length+1}`
- let newReply={id:replyId,username,content};
- existingComment.replies.push(newReply);
- return `You replied successfully`
- }
- }
- toString(sortingType){
- let res=`Title: ${this.title}\n`
- res+=`Creator: ${this.creator}\n`;
- res+=`Likes: ${this.#likes.length}\n`
- res+=`Comments:\n`
- let commentsStr="";
- let commentsArr=[]
- if (sortingType=="asc"){
- this.comments.forEach(c=>{
- commentsStr+=(`-- ${c.id}. ${c.username}: ${c.content}\n`);
- if (c.replies.length>0){
- c.replies.forEach(r=>{
- commentsStr+=`--- ${r.id}. ${r.username}: ${r.content}\n`
- })
- }
- })
- res+=commentsStr.trim();
- }
- if (sortingType=='desc'){
- let descendComments=[];
- for (let i=this.comments.length-1; i>=0; i--){
- let currCom=this.comments[i];
- console.log(currCom)
- currCom.replies.reverse();
- descendComments.push(currCom)
- }
- let commStr=``
- descendComments.forEach(c=>{
- commStr+=`-- ${c.id}. ${c.username}: ${c.content}\n`;
- if (c.replies.length>0){
- c.replies.forEach(r => {
- commStr+=`--- ${r.id}. ${r.username}: ${r.content}\n`
- });
- }
- })
- res+=commStr.trim();
- }
- if (sortingType=="username"){
- let usernameSorted=this.comments.sort((a,b)=>a.username.localeCompare(b.username));
- usernameSorted.forEach(u=>{
- u.replies.sort((a,b)=>a.username.localeCompare(b.username))
- })
- let unStr="";
- usernameSorted.forEach(u=>{
- unStr+=`-- ${u.id}. ${u.username}: ${u.content}\n`;
- if (u.replies.length>0){
- u.replies.forEach(r => {
- unStr+=`--- ${r.id}. ${r.username}: ${r.content}\n`
- });
- }
- })
- res+=unStr.trim();
- }
- return res;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement